Funzione MID VBA

Funzione MID VBA di Excel

La funzione MID VBA estrae i valori dalla metà della frase o della parola fornita. La funzione MID è classificata sotto la funzione String and Text ed è una funzione del foglio di lavoro, il che significa che per utilizzare questa funzione in VBA è necessario utilizzare il metodo application.worksheet.

Ci sono situazioni in cui vogliamo estrarre il nome, il cognome o il secondo nome. In queste situazioni, le formule della categoria TESTO sono utili per soddisfare i nostri requisiti. L'utilizzo di questa funzione è uguale a quello del riferimento del foglio di lavoro e anche la sintassi è la stessa.

Sintassi

Come la nostra funzione excel MID, anche in VBA ha un set simile di valori di sintassi. Di seguito è riportata la sintassi.

  • Stringa da cercare: non è altro che qual è la frase di stringa, ovvero da quale stringa o parola si desidera estrarre i valori.
  • Posizione iniziale: da quale posizione della frase si desidera estrarre. Dovrebbe essere un valore numerico.
  • Numero di caratteri da estrarre: dalla posizione di partenza quanti caratteri vuoi estrarre? Anche questo dovrebbe essere un valore numerico.

Come utilizzare la funzione MID VBA?

Puoi scaricare questo modello di funzione MID VBA qui - Modello di funzione MID VBA

Esempio 1

Supponiamo di avere la parola "Hello Good Morning" e di voler estrarre "Good" da questa frase. Segui i passaggi seguenti per estrarre il valore.

Passaggio 1: creare prima un nome di macro.

Codice:

 Sub MID_VBA_Example1 () End Sub 

Passaggio 2: dichiara una variabile come "STRINGA".

Codice:

 Sub MID_VBA_Example1 () Dim MiddleValue As String End Sub 

Passaggio 3: ora assegna un valore a questa variabile tramite la funzione MID.

Codice:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid (End Sub 

Passaggio 4: il primo argomento è String, ovvero da quale valore si desidera estrarre. Quindi il nostro valore è "Hello Good Morning".

Codice:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hello Good Morning", End Sub 

Passaggio 5: il prossimo è qual è la posizione iniziale del personaggio che si desidera estrarre. In questo caso, il buongiorno parte da un settimo carattere.

Nota: anche lo spazio è un personaggio.

Codice:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hello Good Morning", 7 End Sub 

Passaggio 6: la lunghezza non è altro che il numero di caratteri che desideri estrarre. Dobbiamo estrarre 4 caratteri qui perché la lunghezza della parola "Buono" è di 4 caratteri.

Codice:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hello Good Morning", 7, 4) End Sub 

Passaggio 7: abbiamo completato la formula. Mostriamo il risultato della variabile nella finestra del messaggio.

Codice:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hello Good Morning", 7, 4) MsgBox MiddleValue End Sub 

Passaggio 8: ora esegui questo codice manualmente o premi il tasto F5, la finestra del messaggio dovrebbe mostrare la parola "Buono".

Produzione:

Esempio n. 2

Supponi di avere un nome e un cognome insieme e la parola è "Ramesh, Tendulkar". Tra il nome e il cognome, il carattere di separazione è una virgola (,). Ora dobbiamo estrarre solo il nome.

Passaggio 1: creare una macro e definire una variabile.

Codice:

 Sub MID_VBA_Example2 () Dim FirstName As String End Sub 

Passaggio 2: ora assegna un valore a questa variabile tramite la funzione MID.

Codice:

 Sub MID_VBA_Example2 () Dim FirstName As String FirstName = Mid (End Sub 

Passaggio 3: la nostra stringa è "Ramesh.Tendulkar", quindi inserisci questa parola.

Codice:

 Sub MID_VBA_Example2 () Dim FirstName As String FirstName = Mid ("Ramesh, Tendulkar", End Sub 

Passaggio 4: poiché stiamo estraendo il nome, la posizione iniziale è 1.

Codice:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1, End Sub 

Step 5: Length of the character you can directly enter as 6 but this is not the best way. In order to determine the length lets apply one more formula called Instr.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr( End Sub 

Step 6: For this starting position is 1.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr(1, End Sub 

Step 7: String 1 is our name i.e. “Ramesh, Tendulkar”.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr(1,"Ramesh,Tendulkar", End Sub 

Step 8: String 2 what is the separator of first name & last name i.e. comma (,).

Code:

 Sub MID_VBA_Example2()     Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr(1,"Ramesh,Tendulkar",",") End Sub 

Note: Instr function will return how many characters are there in the word “Ramesh, Tendulkar” from the string 1 position to the string 2 positions i.e. until comma (,). So Instr will return 7 as the result including comma (,).

Step 9: Since Instr function returns no., of characters including comma (,) we need to minus 1 character here. So enter -1 after the close of Instr function.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar", 1, InStr(1, "Ramesh,Tendulkar", ",") - 1) End Sub 

Step 10: Now show the value of the variable in the message box.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar", 1, InStr(1, "Ramesh,Tendulkar", ",") - 1) MsgBox FirstName End Sub 

Step 11: Run this code using F5 key or you can run this code manually, we would get the first name in the message box.

Output:

Example #3

Now I will give you one assignment to solve. I have a list of First Name & Last Name.

From this list I want you to extract the first name only. All the best!!!!.

Ok, If you have tried and not able to get the result then below code would help you in this.

Code:

 Sub MID_VBA_Example3()     Dim   i   As Long For i = 2   To  15 Cells(i, 2).Value = Mid(Cells(i, 1).Value, 1, InStr(1, Cells(i, 1).Value, ",") - 1)     Next i End Sub 

Copy & Paste the above code in your module. After copying the code, run this code using the F5 key or you can run manually.

It should give the result like the below.

Things to Remember

  • Length argument in MID function is optional. If you ignore this it will take 1 as the default value.
  • In order to determine the length or starting position use Instr function along with MID function.