VBA MID funkcija | Kako uporabljati funkcijo Excel VBA MID?

Funkcija Excel VBA MID

VBA MID funkcija izvleče vrednosti iz sredine priloženega stavka ali besede. Funkcija MID je razvrščena pod funkcijo String and Text in je funkcija delovnega lista, kar pomeni, da moramo za uporabo te funkcije v VBA uporabiti metodo application.worksheet.

Obstajajo situacije, ko želimo izluščiti ime, priimek ali priimek. V teh primerih so formule kategorije TEXT koristne za izpolnitev naših zahtev. Uporaba te funkcije je enaka kot pri sklicu na delovni list in tudi sintaksa je enaka.

Sintaksa

Tako kot naša funkcija excel MID ima tudi v VBA podoben nabor sintaksnih vrednosti. Spodaj je sintaksa.

  • String to Search: To ni nič drugega kot kakšen je stavek niza, tj. Iz katerega niza ali besede želite izvleči vrednosti.
  • Začetni položaj: iz katerega položaja stavka želite izvleči. To bi morala biti številčna vrednost.
  • Število znakov, ki jih je treba izvleči : iz začetnega položaja, koliko znakov želite izvleči? To bi morala biti tudi številčna vrednost.

Kako uporabiti funkcijo VBA MID?

To predlogo funkcije VBA MID lahko prenesete tukaj - Predloga funkcije VBA MID

Primer # 1

Recimo, da imate besedo "Hello Good Morning" in želite iz tega stavka izvleči "Good". Sledite spodnjim korakom, da izvlečete vrednost.

1. korak: Najprej ustvarite ime makra.

Koda:

 Sub MID_VBA_Example1 () Konec Sub 

2. korak: Spremenljivko prijavite kot »STRING«.

Koda:

 Sub MID_VBA_Example1 () Dim MiddleValue kot niz na koncu Sub 

3. korak: Zdaj dodelite vrednost tej spremenljivki s funkcijo MID.

Koda:

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

4. korak: Prvi argument je String, tj. Iz katere vrednosti želimo izvleči. Torej, naša vrednost je "Hello Good Morning".

Koda:

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

5. korak: Naslednji je začetni položaj znaka, ki ga želite izvleči. V tem primeru se Dobro jutro začne s 7. znakom.

Opomba: Vesolje je tudi znak.

Koda:

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

6. korak: Dolžina ni nič drugega kot to, koliko znakov želite izvleči. Tu moramo izvleči 4 znake, ker je dolžina besede »Dobro« 4 znake.

Koda:

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

7. korak: Izpolnili smo formulo. Pokažimo rezultat spremenljivke v polju za sporočila.

Koda:

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

8. korak: Zdaj zaženite to kodo ročno ali pritisnite tipko F5, v okencu za sporočilo naj bo prikazana beseda »Dobro«.

Izhod:

2. primer

Recimo, da imate skupaj ime in priimek, beseda pa je "Ramesh, Tendulkar". Med imenom in priimkom je ločilni znak vejica (,). Zdaj moramo izvleči samo prvo ime.

1. korak: Ustvarite makro in definirajte spremenljivko.

Koda:

 Sub MID_VBA_Example2 () Dim FirstName kot niz String Sub 

2. korak: Zdaj dodelite vrednost tej spremenljivki s funkcijo MID.

Koda:

 Sub MID_VBA_Example2 () Dim FirstName kot niz FirstName = Mid (End Sub 

3. korak: Naš niz je "Ramesh.Tendulkar", zato vnesite to besedo.

Koda:

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

4. korak: Ker izvlečemo ime, je začetni položaj 1.

Koda:

 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.