Funzione destra VBA (esempi) - Guida passo passo a Excel VBA a destra

Funzione destra in VBA Excel

La funzione destra è la stessa sia della funzione del foglio di lavoro che di VBA, l'uso di questa funzione è che ci fornisce la sottostringa da una data stringa ma la ricerca viene eseguita da destra a sinistra della stringa, questo è un tipo di funzione stringa in VBA utilizzato con il metodo della funzione application.worksheet.

La funzione DESTRA in Excel VBA viene utilizzata per estrarre i caratteri dal lato destro dei valori di testo forniti. In Excel, abbiamo molte funzioni di testo per gestire i dati basati su testo. Alcune delle funzioni utili sono LEN, LEFT, RIGHT, MID per estrarre i caratteri dai valori di testo. L'esempio comune di utilizzo di queste funzioni è l'estrazione del nome e del cognome separatamente dal nome completo.

La formula DESTRA è presente anche nel foglio di lavoro. In VBA, dobbiamo fare affidamento sulla classe della funzione del foglio di lavoro per accedere alla funzione VBA RIGHT; piuttosto, abbiamo anche la funzione DESTRA incorporata in VBA.

Ora dai un'occhiata alla sintassi seguente della formula VBA RIGHT.

  • Stringa: questo è il nostro valore e da questo valore stiamo cercando di estrarre i caratteri dal lato destro della stringa.
  • Lunghezza: dalla stringa fornita , quanti caratteri ci servono. Se abbiamo bisogno di quattro caratteri dal lato destro, possiamo fornire l'argomento come 4.

Ad esempio, se la stringa è "Cellulare" e se vogliamo estrarre solo la parola "Telefono", possiamo fornire l'argomento come di seguito.

DESTRA ("Telefono cellulare", 5)

Il motivo per cui ho menzionato 5 perché la parola "Telefono" contiene 5 lettere. Nella prossima sezione dell'articolo, vedremo come possiamo usarlo in VBA.

Esempi di funzione destra VBA di Excel

Di seguito sono riportati gli esempi di Right Function VBA Excel.

Esempio 1

Ti mostrerò un semplice esempio per avviare il procedimento. Supponi di avere la stringa "New York" e se desideri estrarre 3 caratteri da destra, segui i passaggi seguenti per scrivere il codice.

Passaggio 1: dichiara la variabile come stringa VBA.

Codice:

Sub Right_Example1 () Dim k As String End Sub

Passaggio 2: ora, per questa variabile, assegneremo il valore applicando la funzione DESTRA.

Codice:

Sub Right_Example1 () Dim k As String k = Right (End Sub

Passaggio 3: il primo argomento è String e la nostra stringa per questo esempio è "New York".

Codice:

Sub Right_Example1 () Dim k As String k = Right ("New York", End Sub

Passaggio 4: il prossimo è il numero di caratteri necessari dalla stringa fornita. In questo esempio, abbiamo bisogno di 3 caratteri.

Codice:

Sub Right_Example1 () Dim k As String k = Right ("New York", 3) End Sub

Passaggio 5: abbiamo 2 argomenti da affrontare, quindi abbiamo finito. Ora assegna il valore di questa variabile nella finestra del messaggio in VBA.

Codice:

Sub Right_Example1 () Dim k As String k = Right ("New York", 3) MsgBox k End Sub

Eseguire il codice utilizzando il tasto F5 o manualmente e vedere il risultato in una finestra di messaggio.

Nella parola "New York" dal lato destro 3 caratteri sono "ork".

Ora cambierò la lunghezza da 3 a 4 per ottenere il valore completo.

Codice:

Sub Right_Example1 () Dim k As String k = Right ("New York", 4) MsgBox k End Sub

Eseguire questo codice manualmente o utilizzando il tasto F5. Quindi, otterremo "York".

Esempio n. 2

Ora dai un'occhiata a un altro esempio, questa volta considera il valore della stringa "Michael Clarke".

Se fornisci la lunghezza come 6, otterremo "Clarke" come risultato.

Codice:

Sub Right_Example1 () Dim k As String k = Right ("Michael Clarke", 6) MsgBox k End Sub

Esegui questo codice utilizzando il tasto F5 o manualmente per vedere il risultato.

Funzione destra dinamica in Excel VBA

If you observe our previous two examples, we have supplied the length argument numbers manually. But this is not the right process to do the job.

In each of the string, right-side characters are different in each case. We cannot refer to the length of the characters manually for each value differently. This where the other string function “Instr” plays a vital role.

Instr function returns the supplied character position in the supplied string value. For example, Instr (1,” Bangalore,” a”) returns the position of the letter “a” in the string “Bangalore” from the first (1) character.

In this case, the result is 2 because from the first character position of the letter “a” is the 2nd position.

If I change the starting position from 1 to 3, it will return 5.

Instr (3,” Bangalore,” a”).

The reason why it returns 5 because I mentioned the starting position to look for the letter “a” only from the 3rd letter. So the position of the second appeared “a” is 5.

So, to find the space character of each string, we can use this. Once we find the space character position, we need to minus that from the total length of the string by using LEN.

For example, in the string “New York,” the total number of characters is 8, including space, and the position of the space character is 4th. So 8-4 = 4 right will extract 4 characters from the right.

Now, look at the below code for your reference.

Code:

Sub Right_Example3() Dim k As String Dim L As String Dim S As String L = Len("Michael Clarke") S = InStr(1, "Michael Clarke", " ") k = Right("Michael Clarke", L - S) MsgBox k End Sub

In the above code variable, “L” will return 14, and the variable “S” will return 8.

In the VBA right formula, I have applied L - S, i.e., 14-8 = 6. So from right side 6 characters, i.e., “Clarke.”

Loops with Right Function in Excel VBA

When we need to apply the VBA RIGHT function with many cells, we need to include this inside the loops. For example, look at the below image.

We cannot apply many lines of the code to extract a string from the right side. So we need to include loops. The below code will do it for the above data.

Code:

Sub Right_Example4 () Dim k As String Dim L As String Dim S As String Dim a As Integer For a = 2 To 5 L = Len (Cells (a, 1) .Value) S = InStr (1, Cells (a, 1 ) .Value, "") Celle (a, 2) .Value = Right (Cells (a, 1), L - S) Next a End Sub

Il risultato di questo codice è il seguente.

Articoli interessanti...