Funzione sinistra VBA - Come utilizzare la funzione sinistra VBA di Excel?

Excel VBA sinistro

VBA Left è anche una delle funzioni del foglio di lavoro classificate sotto le funzioni di testo utilizzate in VBA con l'applicazione. metodo del foglio di lavoro, restituisce il numero di caratteri specificato dalla parte sinistra della stringa.

Alcune delle funzioni di Excel sono integrate anche con VBA. Di tutte le molte funzioni di testo, VBA LEFT è una di quelle funzioni che usiamo abbastanza spesso rispetto ad altre formule.

Se conosci la funzione Excel LEFT, la funzione VBA LEFT è esattamente la stessa. Può estrarre i caratteri dal lato sinistro della stringa o del valore fornito dall'utente.

La sintassi della funzione SINISTRA è esattamente la stessa della funzione del foglio di lavoro.

Ha due argomenti.

  • Stringa: questo nient'altro che il valore o il riferimento di cella. Da questo valore, stiamo cercando di tagliare i personaggi.
  • Lunghezza: quanti caratteri vuoi estrarre dalla stringa che hai fornito. Dovrebbe essere un valore numerico.

Come utilizzare la funzione sinistra VBA di Excel?

Esempio 1

Supponi di avere la parola "Sachin Tendulkar" e desideri solo i primi 6 caratteri di questa parola. Mostreremo come estrarre da sinistra usando la funzione LEFT in VBA.

Passaggio 1: creare un nome macro e definire la variabile come stringa.

Codice:

Sub Left_Example1 () Dim MyValue As String End Sub

Passaggio 2: ora assegna un valore a questa variabile

Codice:

Sub Left_Example1 () Dim MyValue As String MyValue = End Sub

Passaggio 3: aprire la funzione SINISTRA.

Codice:

Sub Left_Example1 () Dim MyValue As String MyValue = Left (End Sub

Passaggio 4: il primo argomento è per dire qual è la stringa o il valore. Il nostro valore qui è "Sachin Tendulkar".

Codice:

Sub Left_Example1 () Dim MyValue As String MyValue = Left ("Sachin Tendulkar", End Sub

Passaggio 5: la lunghezza non è altro che il numero di caratteri che ci servono da sinistra. Abbiamo bisogno di 6 caratteri.

Codice:

Sub Left_Example1 () Dim MyValue As String MyValue = Left ("Sachin Tendulkar", 6) End Sub

Passaggio 6: mostra il valore nel VBA MsgBox.

Codice:

Sub Left_Example1 () Dim MyValue As String MyValue = Left ("Sachin Tendulkar", 6) MsgBox MyValue End Sub

Passaggio 7: eseguire la macro utilizzando il tasto F5 o manualmente tramite un'opzione di esecuzione per ottenere il risultato in una finestra di messaggio.

Produzione:

Invece di mostrare il risultato in una finestra di messaggio, possiamo memorizzare questo risultato in una delle celle del nostro foglio di lavoro Excel. Dobbiamo solo aggiungere il riferimento di cella e il valore della variabile.

Codice:

Sub Left_Example1 () Dim MyValue As String MyValue = Left ("Sachin Tendulkar", 6) Range ("A1"). Value = MyValue End Sub

Ora, se esegui questo codice, otterremo il valore nella cella A1.

Esempio # 2 - SINISTRA con altre funzioni

Nel caso precedente, abbiamo fornito direttamente la lunghezza dei caratteri di cui abbiamo bisogno dalla direzione sinistra, ma questa è più adatta per uno o due valori. Supponiamo che di seguito sia l'elenco dei valori che hai nel tuo foglio Excel.

In each case, first name characters are different from one to another; we cannot directly specify the number of characters we needed from each name.

This is where the beauty of other functions will come into the picture. In order to supply the number of characters dynamically, we can use the “VBA Instr” function.

In the above set of names, we need all the characters from the left until we reach space character. So Instr function can return those many characters.

Step 1: Similarly, start an excel macro name and define a variable as a string.

Code:

Sub Left_Example2() Dim FirstName As String End Sub

Step 2: Assign the value to the variable through the LEFT function.

Code:

Sub Left_Example2() Dim FirstName As String FirstName = Left( End Sub

Step 3: Here, we need to refer the cell to get the value from the cells. So write the code as CELLE (2,1).Value.

Code:

Sub Left_Example2() Dim FirstName As String FirstName = Left(Cells(2,1).Value, End Sub

Step 4: The next thing is how many characters we need. After applying the LEFT function, don’t enter the length of the characters manually. Apply the Instr function.

Code:

Sub Left_Example2() Dim FirstName As String FirstName = Left(Cells(2, 1).Value, InStr(1, Cells(2, 1).Value, " ")) End Sub

Step 5: Store this result in the B2 cell. So the code is CELLS (2,2).value = FirstName

Code:

Sub Left_Example2() Dim FirstName As String FirstName = Left(Cells(2, 1).Value, InStr(1, Cells(2, 1).Value, " ")) Cells(2, 2).Value = FirstName End Sub

Passaggio 6: eseguire questo codice manualmente o tramite F5, otterremo il nome.

Abbiamo il primo nome per un nome, ma abbiamo anche molti altri nomi. Non possiamo scrivere 100 righe di codice da estrarre, quindi come estraiamo?

È qui che entra in gioco la bellezza dei loop. Di seguito è riportato il codice del ciclo, che può eliminare tutti i passaggi indesiderati e può svolgere il lavoro in 3 righe.

Codice:

Sub Left_Example2 () Dim FirstName As String Dim i As Integer For i = 2 To 9 FirstName = Left (Cells (i, 1) .Value, InStr (1, Cells (i, 1) .Value, "") - 1) Celle (i, 2) .Value = FirstName Next i End Sub
Nota: la funzione Instr restituisce anche il carattere spazio, quindi è necessario meno 1 dal risultato Instr.

Se esegui questo codice, otterremo i primi valori del nome.

Produzione:

Cose da ricordare

  • La sinistra può estrarre solo da sinistra.
  • La funzione VBA Instr trova la posizione del carattere in fornito nella stringa.

Articoli interessanti...