VBA MID-functie - Hoe Excel VBA MID-functie te gebruiken?

Inhoudsopgave

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 String and Text-functie en het is een werkbladfunctie, wat betekent dat om deze functie in VBA te gebruiken, we de methode application.worksheet moeten 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 deze 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 tekens dat moet worden geëxtraheerd : vanaf de startpositie, hoeveel tekens wilt u extraheren? Dit moet ook een numerieke waarde zijn.

Hoe de VBA MID-functie te gebruiken?

Voorbeeld 1

Stel dat u het woord "Hallo, goedemorgen" hebt, en u 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 de MID-functie.

Code:

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

Stap 4: Het eerste argument is String, dat wil zeggen, 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 stap is wat de startpositie is van het personage dat u wilt extraheren. In dit geval begint Goedemorgen met een 7 de 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 weergeven.

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 de 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

Step 4: Since we are extracting the first name starting position is 1.

Code:

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, let’s 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 a 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 the 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 the below code would help you in this.

Code:

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

Kopieer en plak de bovenstaande code in uw module. Nadat u de code hebt gekopieerd, voert u deze code uit met de F5-toets, of u kunt deze handmatig uitvoeren.

Het zou een resultaat moeten geven zoals het onderstaande.

Dingen om te onthouden

  • Het lengte-argument in de MID-functie is optioneel. Als u dit negeert, wordt 1 als standaardwaarde gebruikt.
  • Gebruik de functie Instr samen met de functie MID om de lengte of startpositie te bepalen.

Interessante artikelen...