Sottostringa VBA

Sottostringa VBA di Excel

Sottostringa è una parte della stringa o parte o il carattere della stringa è chiamato "Sottostringa". Ci sono tre tipi di funzione di sottostringa in VBA LEFT, RIGHT e MID sono simili alle sottostringhe del foglio di lavoro in Excel.

Una stringa non è altro che una serie di caratteri e i caratteri potrebbero essere alfabeti, numeri, caratteri speciali e anche combinare tutti questi.

Spesso in Excel quando lavoriamo con dati che sono la stringa di cui abbiamo bisogno per ottenere solo la parte della stringa per facilitare il nostro scopo. Potremmo non aver bisogno della stringa completa da utilizzare, ma abbiamo bisogno solo della parte della stringa per il nostro uso. Ad esempio, se hai il nome "Sachin Tendulkar" potresti aver bisogno solo della prima parte del nome, cioè solo "Sachin". Questo è chiamato come sottostringa della stringa in Excel VBA. Per gestire queste stringhe abbiamo funzioni integrate nella funzione TESTO nella categoria Excel.

In questo articolo, discuteremo come ottenere la sottostringa dalla stringa completa in VBA.

Come utilizzare le funzioni di sottostringa in VBA?

Per estrarre la sottostringa dalla stringa abbiamo alcune delle funzioni di testo incorporate e alcune delle funzioni importanti sono LEFT, RIGHT, INSTR e MID in Excel. La funzione Instr fungerà da funzione di supporto per le altre tre funzioni.

Vedremo come utilizzare queste funzioni per estrarre praticamente le sottostringhe. Leggi gli esempi seguenti per capirli.

Puoi scaricare questo modello VBA SubString Excel qui - Modello VBA SubString Excel

Esempio n. 1: utilizzo della funzione sinistra

Se hai il nome completo "Sachin Tendulkar" e hai bisogno solo del nome da estrarre come sottostringa, usa il codice seguente per ottenere lo stesso.

Passaggio 1: creare un nome di macro e definire due variabili come String.

Codice:

 Sub SubString_Example1 () Dim FullName As String Dim FirstName As String End Sub 

Passaggio 2: Ora assegna il nome "Sachin Tendulkar" alla variabile FullName .

Codice:

 Sub SubString_Example1 () Dim FullName As String Dim FirstName As String FullName = "Sachin Tendulkar" End Sub 

Passaggio 3: ora la variabile FullName contiene il valore di "Sachin Tendulkar". Ora dobbiamo estrarre la sottostringa VBA di Excel del nome dal nome completo. Quindi, assegna il valore per la variabile FirstName tramite la funzione LEFT.

Codice:

 Sub SubString_Example1 () Dim FullName As String Dim FirstName As String FullName = "Sachin Tendulkar" FirstName = Left (End Sub 

Passaggio 4: il primo argomento della funzione VBA SINISTRA è String, ovvero il valore completo o la stringa completa. In questo esempio, il nostro valore o stringa completo è "Sachin Tendulkar" che è assegnato alla variabile FullName.

Quindi fornire la variabile FullName come argomento.

Codice:

 Sub SubString_Example1 () Dim FullName As String Dim FirstName As String FullName = "Sachin Tendulkar" FirstName = Left End Sub 

Passo 5: Il prossimo  argomento è quanti caratteri abbiamo bisogno dalla stringa che abbiamo fornito, quindi in questo caso, abbiamo bisogno del primo nome " Sachin ", quindi in totale abbiamo bisogno di 6 caratteri dal lato sinistro.

Codice:

 Sub SubString_Example1 () Dim FullName As String Dim FirstName As String FullName = "Sachin Tendulkar" FirstName = Left (FullName, 6) End Sub 

Passaggio 6: ora mostra il risultato in una finestra di messaggio in VBA.

Codice:

 Sub SubString_Example1 () Dim FullName As String Dim FirstName As String FullName = "Sachin Tendulkar" FirstName = Left (FullName, 6) MsgBox FirstName End Sub 

Passaggio 7: eseguire la macro, vedere il nome come sottostringa nella finestra del messaggio.

Esempio # 2: ottieni la sottostringa da destra

Analogamente a come abbiamo estratto la sottostringa da sinistra, allo stesso modo possiamo estrarre anche da destra. Prendi lo stesso nome come esempio.

Passaggio 1: definire due variabili come String.

Codice:

 Sub SubString_Example2 () Dim FullName As String Dim LastName As String End Sub 

Passaggio 2: come al solito, assegna il valore alla variabile FullName come "Sachin Tendulkar"

Codice:

 Sub SubString_Example2 () Dim FullName As String Dim LastName As String FullName = "Sachin Tendulkar" End Sub 

Passaggio 3: ora per la variabile LastName assegnare il valore tramite la funzione Excel DESTRA.

Codice:

 Sub SubString_Example2 () Dim FullName As String Dim LastName As String FullName = "Sachin Tendulkar" LastName = Right (End Sub 

Passaggio 4: la stringa è il nostro FullName, quindi fornire la variabile.

Codice:

 Sub SubString_Example2 () Dim FullName As String Dim LastName As String FullName = "Sachin Tendulkar" LastName = Right (FullName, End Sub 

Passaggio 5: la lunghezza è il numero di caratteri necessari dal lato destro, abbiamo bisogno di 9 caratteri dal lato destro.

Codice:

 Sub SubString_Example2 () Dim FullName As String Dim LastName As String FullName = "Sachin Tendulkar" LastName = Right (FullName, 9) End Sub 

Passaggio 6: mostra questo valore nella finestra del messaggio .

Codice:

 Sub SubString_Example2 () Dim FullName As String Dim LastName As String FullName = "Sachin Tendulkar" LastName = Right (FullName, 9) MsgBox LastName End Sub 

Passaggio 7: eseguire la macro, vedremo il cognome nella finestra del messaggio.

Esempio # 3 - Utilizzo della funzione Instr

In the above examples, we had only one name and we have directly supplied how many characters we need from the left & right. But in case of many names first name & last name characters are not the same, it will differ from name to name. In those cases, we cannot supply the number of characters directly so we can use the function Instr.

Instr function will return the supplied character position in the string. For an example look at the below code.

Code:

 Sub SubString_Example3() Dim Position As String Position = InStr(1, "Sachin", "a") MsgBox Position End Sub 

InStr(1, “Sachin”, “a”) this will identify the position of the letter “a” as the first appearance in the string “Sachin”. In this case letter “a” is in the second position. So we will get 2 as the result in the message box.

Like this, we can use the Instr function to find the space character between the first name and last name.

For an example look at the below name I have in excel sheet.

Using LEFT, RIGHT, and Instr function we can extract the substrings. Below is the code to extract the First Name.

Code:

 Sub FirstName() Dim K As Long Dim LR As Long LR = Cells(Rows.Count, 1).End(xIUp).Row For K = 2 To LR Cells(K, 2).Value = Left(Cells(K, 1).Value, InStr(1, Cells(K, 1).Value, "") - 1) Next K End Sub 

Run the macro and see the first name as a substring in the message box.

Use below code to extract the last name as a substring.

Code:

 Sub LastName() Dim K As Long Dim LR As Long LR = Cells(Rows.Count, 1).End(xIUp).Row For K = 2 To LR Cells(K, 3).Value = Right(Cells(K, 1).Value, Len(Cells(K, 1)) - InStr(1, Cells(K, 1).Value, "")) Next K End Sub 

Run the macro and we will see the last name in the message box.

I have assigned the macro button to the worksheet, download the workbook, and use them.