Sottostringa VBA - Come estrarre la sottostringa utilizzando le funzioni 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 possono essere alfabeti, numeri, caratteri speciali e anche combinare tutti questi.

Spesso in Excel, quando lavoriamo con i dati, che è la stringa, abbiamo bisogno di ottenere solo la parte della stringa per facilitare il nostro scopo. Potremmo non aver bisogno della stringa completa da usare, 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, ovvero solo "Sachin". Questo è chiamato come sottostringa della stringa in Excel VBA. Per gestire queste stringhe, abbiamo funzioni integrate sotto la 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.

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; questo è 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

Passaggio 5: Il prossimo argomento è di quanti caratteri abbiamo bisogno dalla stringa che abbiamo fornito, quindi in questo caso, abbiamo bisogno del primo nome " Sachin " così totalmente, 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 in modo simile, 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, assegna 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 sul 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

Negli esempi precedenti, avevamo un solo nome e abbiamo fornito direttamente quanti caratteri ci servono da sinistra e destra. Ma nel caso di molti nomi, i caratteri del nome e del cognome non sono gli stessi, saranno diversi da nome a nome. In questi casi, non possiamo fornire direttamente il numero di caratteri, quindi possiamo usare la funzione Instr.

La funzione Instr restituirà la posizione del carattere fornita nella stringa. Ad esempio, guarda il codice seguente.

Codice:

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 a 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 example, look at the below name I have in the 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

Eseguire la macro e visualizzare il nome come sottostringa nella finestra del messaggio.

Usa il codice seguente per estrarre il cognome come sottostringa.

Codice:

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

Esegui la macro e vedremo il cognome nella finestra del messaggio.

Ho assegnato il pulsante macro al foglio di lavoro, ho scaricato la cartella di lavoro e li ho utilizzati.

Articoli interessanti...