ฟังก์ชัน Excel VBA MID
ฟังก์ชันVBA MIDจะดึงค่าจากตรงกลางของประโยคหรือคำที่ให้มา ฟังก์ชัน MID ถูกจัดหมวดหมู่ภายใต้ฟังก์ชันสตริงและข้อความและเป็นฟังก์ชันแผ่นงานซึ่งหมายความว่าจะใช้ฟังก์ชันนี้ใน VBA เราจำเป็นต้องใช้วิธีการ application.worksheet
มีสถานการณ์ที่เราต้องการแยกชื่อนามสกุลหรือชื่อกลาง ในสถานการณ์เหล่านั้นสูตรหมวดหมู่ TEXT มีประโยชน์ในการตอบสนองความต้องการของเรา การใช้ฟังก์ชันนี้เหมือนกับการอ้างอิงแผ่นงานและไวยากรณ์ก็เหมือนกัน
ไวยากรณ์
เช่นเดียวกับฟังก์ชัน excel MID ของเราใน VBA ก็มีชุดค่าไวยากรณ์ที่คล้ายกัน ด้านล่างนี้คือไวยากรณ์
- String to Search:นี่คืออะไร แต่เป็นประโยคของสตริงเช่นสตริงหรือคำที่คุณต้องการแยกค่า
- ตำแหน่งเริ่มต้น:จากตำแหน่งใดของประโยคที่คุณต้องการแยก ค่านี้ควรเป็นค่าตัวเลข
- จำนวนอักขระที่จะแยก:จากตำแหน่งเริ่มต้นคุณต้องการแยกอักขระจำนวนเท่าใด ค่านี้ควรเป็นค่าตัวเลขด้วย
จะใช้ฟังก์ชัน VBA MID ได้อย่างไร?
คุณสามารถดาวน์โหลดเทมเพลตฟังก์ชัน VBA MID ได้ที่นี่ - เทมเพลตฟังก์ชัน VBA MIDตัวอย่าง # 1
สมมติว่าคุณมีคำว่า "สวัสดีตอนเช้า" และคุณต้องการแยกคำว่า "Good" ออกจากประโยคนี้ ทำตามขั้นตอนด้านล่างเพื่อแยกค่า
ขั้นตอนที่ 1:สร้างชื่อมาโครก่อน
รหัส:
ย่อย MID_VBA_Example1 () End Sub
ขั้นตอนที่ 2:ประกาศตัวแปรเป็น“ STRING”
รหัส:
ย่อย MID_VBA_Example1 () Dim MiddleValue เป็น String End Sub
ขั้นตอนที่ 3:กำหนดค่าให้กับตัวแปรนี้ผ่านฟังก์ชัน MID
รหัส:
Sub MID_VBA_Example1 () Dim MiddleValue เป็น String MiddleValue = Mid (End Sub
ขั้นตอนที่ 4:อาร์กิวเมนต์แรกคือ String เช่นซึ่งเราต้องการแยกค่า ดังนั้นคุณค่าของเราคือ“ สวัสดีตอนเช้า”
รหัส:
Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("สวัสดีตอนเช้า", End Sub
ขั้นตอนที่ 5:ถัดไปคือตำแหน่งเริ่มต้นของตัวละครที่คุณต้องการแยกออกมา ในกรณีนี้อรุณสวัสดิ์จะเริ่มจากอักขระตัวที่ 7
หมายเหตุ: Space เป็นอักขระเช่นกัน
รหัส:
Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("สวัสดีตอนเช้า", 7 End Sub
ขั้นตอนที่ 6:ความยาวคืออะไรนอกจากจำนวนอักขระที่คุณต้องการแยก เราจำเป็นต้องแยกอักขระ 4 ตัวตรงนี้เนื่องจากความยาวของคำว่า "ดี" คือ 4 อักขระ
รหัส:
Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("สวัสดีอรุณสวัสดิ์", 7, 4) End Sub
ขั้นตอนที่ 7:เราทำสูตรสำเร็จแล้ว มาแสดงผลลัพธ์ของตัวแปรในกล่องข้อความ
รหัส:
Sub MID_VBA_Example1 () Dim MiddleValue As String MiddleValue = Mid ("สวัสดีอรุณสวัสดิ์", 7, 4) MsgBox MiddleValue End Sub
ขั้นตอนที่ 8:เรียกใช้รหัสนี้ด้วยตนเองหรือกดปุ่ม F5 กล่องข้อความควรแสดงคำว่า“ ดี”
เอาท์พุต:
ตัวอย่าง # 2
Assume you have a first name and last name together and the word is “Ramesh, Tendulkar”. Between First Name & Last Name, separation character is a comma (,). Now we need to extract the first name only.
Step 1: Create a macro and define a variable.
Code:
Sub MID_VBA_Example2() Dim FirstName As String End Sub
Step 2: Now assign a value to this variable through MID function.
Code:
Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid( End Sub
Step 3: Our string is “Ramesh.Tendulkar”, so enter this word.
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 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.