VBA MID-functie

Excel VBA MID-functie

De VBA MID- functie haalt de waarden uit het midden van de opgegeven zin of woord. De MID-functie is gecategoriseerd onder de tekenreeks- en tekstfunctie en het is een werkbladfunctie, wat betekent dat we deze functie in VBA moeten gebruiken, we moeten de methode application.worksheet gebruiken.

Er zijn situaties waarin we de voornaam, achternaam of middelste naam willen extraheren. In die situaties zijn TEXT-categorieformules handig om aan onze vereisten te voldoen. Het gebruik van deze functie is hetzelfde als die van de werkbladverwijzing en de syntaxis is ook hetzelfde.

Syntaxis

Net als onze Excel MID-functie, heeft het ook in VBA een vergelijkbare set syntaxiswaarden. Hieronder staat de syntaxis.

  • Tekenreeks om te zoeken: dit is niets anders dan de zin van de tekenreeks, dwz uit welke tekenreeks of welk woord u de waarden wilt extraheren.
  • Startpositie: vanaf welke positie van de zin u wilt extraheren. Dit moet een numerieke waarde zijn.
  • Aantal te extraheren tekens : vanaf de startpositie hoeveel tekens u wilt extraheren? Dit moet ook een numerieke waarde zijn.

Hoe de VBA MID-functie te gebruiken?

U kunt deze VBA MID-functiesjabloon hier downloaden - VBA MID-functiesjabloon

Voorbeeld 1

Stel dat je het woord "Hallo, goedemorgen" hebt en je wilt "Goed" uit deze zin halen. Volg de onderstaande stappen om de waarde te extraheren.

Stap 1: Maak eerst een macronaam.

Code:

 Sub MID_VBA_Example1 () Einde Sub 

Stap 2: Declareer een variabele als "STRING".

Code:

 Sub MID_VBA_Example1 () Dim MiddleValue As String End Sub 

Stap 3: Wijs nu een waarde toe aan deze variabele via MID-functie.

Code:

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

Stap 4: Het eerste argument is String, dwz waaruit we willen extraheren. Dus onze waarde is "Hallo, goedemorgen".

Code:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hallo goedemorgen", End Sub 

Stap 5: De volgende is wat de startpositie is van het personage dat u wilt extraheren. In dit geval begint Goedemorgen met een 7e teken.

Opmerking: spatie is ook een personage.

Code:

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

Stap 6: Lengte is niets anders dan het aantal tekens dat u wilt extraheren. We moeten hier 4 karakters extraheren omdat de lengte van het woord "Goed" 4 karakters is.

Code:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hallo goedemorgen", 7, 4) End Sub 

Stap 7: We hebben de formule voltooid. Laten we het resultaat van de variabele in het berichtvenster laten zien.

Code:

 Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("Hallo goedemorgen", 7, 4) MsgBox MiddleValue End Sub 

Stap 8: Voer deze code nu handmatig uit of druk op de F5-toets, het berichtvenster zou het woord "Goed" moeten tonen.

Uitgang:

Voorbeeld # 2

Stel dat je een voornaam en achternaam samen hebt en het woord is "Ramesh, Tendulkar". Tussen voornaam en achternaam is het scheidingsteken een komma (,). Nu hoeven we alleen de voornaam te extraheren.

Stap 1: Maak een macro en definieer een variabele.

Code:

 Sub MID_VBA_Example2 () Dim Voornaam As String End Sub 

Stap 2: Wijs nu een waarde toe aan deze variabele via MID-functie.

Code:

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

Stap 3: Onze string is "Ramesh.Tendulkar", dus voer dit woord in.

Code:

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

Stap 4: Aangezien we extraheren, is de startpositie van de voornaam 1.

Code:

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

Stap 5: Lengte van het teken dat u direct kunt invoeren als 6, maar dit is niet de beste manier. Om de lengte te bepalen, passen we nog een formule toe genaamd Instr.

Code:

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

Stap 6: Voor deze startpositie is 1.

Code:

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

Stap 7: String 1 is onze naam dwz "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.