ไปยังเนื้อหาหลัก

วิธีเติม textbox ตามการเลือก combobox บน UserForm ใน Excel

สมมติว่าคุณมีตารางประกอบด้วยชื่อและคอลัมน์หมายเลขพนักงานตามภาพด้านล่างที่แสดง ตอนนี้คุณต้องสร้าง Userform ที่ฝังด้วยกล่องคำสั่งผสมซึ่งรวบรวมชื่อทั้งหมดไว้ภายในและกล่องข้อความซึ่งจะเติมหมายเลขพนักงานที่เกี่ยวข้องเมื่อเลือกชื่อจากกล่องคำสั่งผสม วิธีการในบทความนี้สามารถช่วยคุณได้

เติมช่องข้อความตามการเลือก combobox บน Userform ด้วย VBA


เติมช่องข้อความตามการเลือก combobox บน Userform ด้วย VBA

โปรดทำดังนี้เพื่อเติมช่องข้อความด้วยค่าที่สอดคล้องกันตามการเลือก combobox บน Userform

1 กด อื่น ๆ + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคลิก สิ่งที่ใส่เข้าไป > แบบฟอร์มผู้ใช้. ดูภาพหน้าจอ:

3. จากนั้นแทรกตัวควบคุมกล่องคำสั่งผสมและตัวควบคุมกล่องข้อความลงใน Userform ที่สร้างขึ้นดังภาพด้านล่างที่แสดง

4. คลิกขวาบนพื้นที่ว่างของ Userform จากนั้นคลิก ดูรหัส จากเมนูบริบท ดูภาพหน้าจอ:

5. ในหน้าต่าง Userform (Code) ที่เปิดขึ้นมาโปรดคัดลอกและวางโค้ด VBA ด้านล่างเพื่อแทนที่ต้นฉบับ

รหัส VBA: เติมช่องข้อความตามการเลือก combobox บน Userform

Dim xRg As Range
'Updated by Extendoffice 2018/1/30
Private Sub UserForm_Initialize()
    Set xRg = Worksheets("Sheet5").Range("A2:B8")
    Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub ComboBox1_Change()
    Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, xRg, 2, False)
End Sub

หมายเหตุ: ในโค้ด A2:B2 คือช่วงที่มีค่าที่คุณจะเติมข้อมูลแยกกันทั้งในกล่องคำสั่งผสม Userform และกล่องข้อความ และ Sheet5 คือชื่อของแผ่นงานที่มีช่วงข้อมูล กรุณาเปลี่ยนตามที่คุณต้องการ

6 กด อื่น ๆ + Q ปุ่มเพื่อออกจากไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

7 คลิก ผู้พัฒนา > สิ่งที่ใส่เข้าไป > ปุ่มคำสั่ง (ActiveX Control) เพื่อแทรกปุ่มคำสั่งลงในแผ่นงาน

8. คลิกขวาที่แท็บแผ่นงานแล้วเลือก ดูรหัส จากเมนูคลิกขวา จากนั้นคัดลอกและวางโค้ด VBA ลงในหน้าต่างโค้ด

รหัส VBA: แสดงรูปแบบผู้ใช้

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2018/1/30
    UserForm1.Show
End Sub

9. ปิดโหมดการออกแบบในสมุดงาน

คลิกปุ่มคำสั่งเพื่อเปิดฟอร์มผู้ใช้ที่ระบุ จากนั้นคุณจะเห็นชื่อทั้งหมดที่ถูกรวบรวมไว้ในกล่องคำสั่งผสม เมื่อเลือกชื่อจากกล่องคำสั่งผสม หมายเลขพนักงานที่เกี่ยวข้องจะถูกเติมลงในกล่องข้อความโดยอัตโนมัติดังภาพด้านล่างนี้


บทความที่เกี่ยวข้อง:

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
can the same be don on excel sheet? instead of userform? i am using sheet not userform
This comment was minimized by the moderator on the site
alguem poderia me ajudar estou tentando carregar a combobox e puxar informações para text box so que esta dando erro 381

Private Sub ComboNome_Emissor_Change()
TextArea.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 1)
TextTreinamento_PT.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 3)
TextTreinamento_EC.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 5)
TexStatus_Bloqueio.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 6)


End Sub
This comment was minimized by the moderator on the site
The method is very simple and populates the combo box quite efficiently. However, when I try populating the text box, I get a run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class. Kindly assist with resolving this error.
This comment was minimized by the moderator on the site
I just tried this code and was running into the same issue. I realized I had the xRg variable inside the UserForm block, so it was not being accessed by my ComboBox function. Maybe others are running into the same issue
This comment was minimized by the moderator on the site
Hi,
May I have your Office version? I need the feedback to check for the error.
Thanks for your comment.
This comment was minimized by the moderator on the site
office 2010 32 bit
This comment was minimized by the moderator on the site
Good day,
I have tried the code in Office 2010 32 bit, but no errors occured.
Please make sure the sheet name and cell range in the first VBA code are the same with your actual sheet name and range.
Thanks for your comment.
This comment was minimized by the moderator on the site
Hi,
i have same issue with office 2013. Any solution please
This comment was minimized by the moderator on the site
i have the same problem....
This comment was minimized by the moderator on the site
I too have the same problem
This comment was minimized by the moderator on the site
Hi Chaylon,
Have you replaced the sheet name in the below code line with your actual used worksheet name?
Set xRg = Worksheets("Sheet5").Range("A2:B8")
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations