ฟังก์ชัน VBA ขวา (ตัวอย่าง) | คำแนะนำทีละขั้นตอนสำหรับ Excel VBA ขวา

ฟังก์ชันขวาใน VBA Excel

ฟังก์ชันขวาจะเหมือนกับในฟังก์ชันแผ่นงานและ VBA การใช้ฟังก์ชันนี้คือให้สตริงย่อยจากสตริงที่กำหนด แต่การค้นหาจะทำจากขวาไปซ้ายของสตริงซึ่งเป็นฟังก์ชันสตริงประเภทหนึ่งใน VBA ใช้กับวิธีฟังก์ชัน application.worksheet

ฟังก์ชัน RIGHT ใน Excel VBA ซึ่งใช้ในการแยกอักขระจากด้านขวาของค่าข้อความที่ให้มา ใน excel เรามีฟังก์ชันข้อความมากมายเพื่อจัดการกับข้อมูลที่เป็นข้อความ ฟังก์ชันที่มีประโยชน์บางอย่าง ได้แก่ ฟังก์ชัน LEN, LEFT, RIGHT, MID เพื่อแยกอักขระจากค่าข้อความ ตัวอย่างทั่วไปของการใช้ฟังก์ชันเหล่านี้คือการแยกชื่อและนามสกุลแยกจากชื่อเต็ม

สูตร RIGHT ก็มีอยู่ในแผ่นงานเช่นกัน ใน VBA เราจำเป็นต้องพึ่งพาคลาสฟังก์ชันแผ่นงานเพื่อเข้าถึงฟังก์ชัน VBA RIGHT แต่เรามีฟังก์ชัน RIGHT ในตัวใน VBA เช่นกัน

ลองดูไวยากรณ์ด้านล่างของสูตร VBA RIGHT

  • String:นี่คือค่าของเราและจากค่านี้เรากำลังพยายามดึงอักขระจากด้านขวาของสตริง
  • ความยาว:จากสตริงที่ให้มาจำนวนอักขระที่เราต้องการ หากเราต้องการอักขระสี่ตัวจากด้านขวาเราสามารถระบุอาร์กิวเมนต์เป็น 4 ได้

ตัวอย่างเช่นหากสตริงคือ "โทรศัพท์มือถือ" และหากเราต้องการแยกเฉพาะคำว่า "โทรศัพท์" เราสามารถระบุอาร์กิวเมนต์ดังต่อไปนี้

ขวา (“ โทรศัพท์มือถือ”, 5)

สาเหตุที่ผมพูดถึง 5 เพราะคำว่า“ โทรศัพท์” มีตัวอักษร 5 ตัวอยู่ในนั้น ในส่วนถัดไปของบทความเราจะดูว่าเราสามารถใช้งานได้อย่างไรใน VBA

ตัวอย่างฟังก์ชัน Excel VBA Right

ต่อไปนี้เป็นตัวอย่างของ Right Function VBA Excel

คุณสามารถดาวน์โหลดเทมเพลต Excel VBA Right Function ได้ที่นี่ - เทมเพลต Excel VBA Right Function

ตัวอย่าง # 1

ฉันจะแสดงตัวอย่างง่ายๆเพื่อเริ่มต้นการดำเนินคดี สมมติว่าคุณมีสตริง "New York" และหากคุณต้องการแยกอักขระ 3 ตัวจากด้านขวาให้ทำตามขั้นตอนด้านล่างเพื่อเขียนโค้ด

ขั้นตอนที่ 1:ประกาศตัวแปรเป็น VBA String

รหัส:

 Sub Right_Example1 () Dim k เป็น String End Sub 

ขั้นตอนที่ 2:สำหรับตัวแปรนี้เราจะกำหนดค่าโดยใช้ฟังก์ชัน RIGHT

รหัส:

 Sub Right_Example1 () Dim k As String k = Right (End Sub 

ขั้นตอนที่ 3:  อาร์กิวเมนต์แรกคือStringและสตริงของเราสำหรับตัวอย่างนี้คือ "New York"

รหัส:

 Sub Right_Example1 () Dim k As String k = Right ("New York", End Sub 

ขั้นตอนที่ 4:ถัดไปคือจำนวนอักขระที่เราต้องการจากสตริงที่ให้มา ในตัวอย่างนี้เราต้องการ 3 ตัวอักษร

รหัส:

 Sub Right_Example1 () Dim k As String k = Right ("New York", 3) End Sub 

ขั้นตอนที่ 5:เรามี 2 ข้อโต้แย้งที่ต้องจัดการดังนั้นเราจึงเสร็จสิ้น ตอนนี้กำหนดค่าของตัวแปรนี้ในกล่องข้อความใน VBA

รหัส:

 Sub Right_Example1 () Dim k As String k = Right ("New York", 3) MsgBox k End Sub 

เรียกใช้รหัสโดยใช้ปุ่ม F5 หรือด้วยตนเองและดูผลลัพธ์ในกล่องข้อความ

ในคำว่า "New York" จากด้านขวา 3 ตัวอักษรคือ"ork"

ตอนนี้ฉันจะเปลี่ยนความยาวจาก 3 เป็น 4 เพื่อให้ได้ค่าเต็ม

รหัส:

 Sub Right_Example1 () Dim k As String k = Right ("New York", 4) MsgBox k End Sub 

เรียกใช้รหัสนี้ด้วยตนเองหรือใช้ปุ่ม F5 จากนั้นเราจะได้รับ“ York”

ตัวอย่าง # 2

มาดูอีกตัวอย่างหนึ่งคราวนี้ให้พิจารณาค่าสตริงเป็น“ Michael Clarke”

หากคุณระบุความยาวเป็น 6 เราจะได้ "Clarke" เป็นผลลัพธ์

รหัส:

 Sub Right_Example1 () Dim k As String k = Right ("Michael Clarke", 6) MsgBox k End Sub 

เรียกใช้รหัสนี้โดยใช้ปุ่ม F5 หรือด้วยตนเองเพื่อดูผลลัพธ์

ฟังก์ชันขวาแบบไดนามิกใน Excel VBA

หากคุณสังเกตสองตัวอย่างก่อนหน้านี้เราได้ระบุหมายเลขอาร์กิวเมนต์ความยาวด้วยตนเอง แต่นี่ไม่ใช่กระบวนการที่ถูกต้องในการทำงาน

In each of the string right-side characters are different in each case. We cannot refer to the length of the characters manually for each value differently. This where the other string function “Instr” plays a vital role.

Instr function returns the supplied character position in the supplied string value. For example, Instr (1,” Bangalore”,”a”) return the position of the letter “a” in the string “Bangalore” from the first (1) character.

In this case, the result is 2 because from the first character position of the letter “a” is the 2nd position.

If I change the starting position from 1 to 3 it will return 5.

Instr (3,” Bangalore”,”a”).

The reason why it returns 5 because I mentioned the starting position to look for the letter “a” only from the 3rd letter. So the position of the second appeared “a” is 5.

So, to find the space character of the of each string we can use this. Once we find the space character position we need to minus that from a total length of the string by using LEN.

For example in the string “New York” the total number of characters is 8 including space, and the position of the space character is 4th. So 8-4 = 4 right will extract 4 characters from the right.

Now, look at the below code for your reference.

Code:

 Sub Right_Example3() Dim k As String Dim L As String Dim S As String L = Len("Michael Clarke") S = InStr(1, "Michael Clarke", " ") k = Right("Michael Clarke", L - S) MsgBox k End Sub 

In the above code variable “L” will return 14 and variable “S” will return 8.

In the VBA right formula, I have applied L – S i.e. 14-8 = 6. So from right side 6 characters i.e. “Clarke”.

Loops with Right Function in Excel VBA

When we need to apply the VBA RIGHT function with many cells we need to include this inside the loops. For example, look at the below image.

We cannot apply many lines of the code to extract a string from the right side. So we need to include loops. The below code will do it for the above data.

Code:

 Sub Right_Example4() Dim k As String Dim L As String Dim S As String Dim a As Integer For a = 2 To 5 L = Len(Cells(a, 1).Value) S = InStr(1, Cells(a, 1).Value, " ") Cells(a, 2).Value = Right(Cells(a, 1), L - S) Next a End Sub 

The result of this code is as follows.