Data Validation (การตรวจสอบข้อมูล) เป็นฟีเจอร์น่าสนใจของ Excel ที่ควบคุมการป้อนข้อมูลในเซลล์ ผู้ใช้ต้องเลือกจากรายการที่กำหนด ไม่สามารถพิมพ์ค่าใดๆ ได้ บทความนี้จะแนะนำวิธีทำ Autocomplete Data Validation drop-down list ใน Excel
2 วิธีสำหรับ Autocomplete Data Validation Drop-Down List ใน Excel
จะแสดง 2 วิธีที่แตกต่างกัน พร้อมชุดข้อมูลตัวอย่าง
1. Autocomplete ด้วยรหัส VBA ใน Combo Box Control (ActiveX)
ใช้โค้ด VBA กับเครื่องมือ ActiveX Control เพื่อทำการตรวจสอบข้อมูลจากรายการแบบเลื่อนลงอัตโนมัติ
ขั้นตอนที่ 1: เพิ่มแท็บ Developer- ไปที่ File > Options
- เลือก Customize Ribbon
- ติ๊กเลือก Developer แล้วกด OK
- ที่แท็บ Developer เลือก Insert
- เลือก Combo Box จาก ActiveX Control
- วาง Control box บนข้อมูล
- คลิกขวาเลือก Properties
- เปลี่ยน Name เป็น TempComboBox
- ที่ชีท คลิกขวาเลือก View Code
Private Sub Wrksht_SelectionChange(ByVal Target As Range)
Dim combox_1 As OLEObject
Dim str_1 As String
Dim ws_1 As Worksheet
Dim arr_1
Set ws_1 = Application.ActiveSheet
On Error Resume Next
Set combox_1 = ws_1.OLEObjects("TempComboBox")
With combox_1
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
If Target.Validation.Type = 3 Then
Target.Validation.InCellDropdown = False
Cancel = True
str_1 = Target.Validation.Formula1
str_1 = Right(str_1, Len(str_1) - 1)
If str_1 = "" Then Exit Sub
With combox_1
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str_1
If .ListFillRange = "" Then
arr_1 = Split(str_1, ",")
Me.TempComboBox.List = arr_1
End If
.LinkedCell = Target.Address
End With
combox_1.Activate
Me.TempComboBox.DropDown
End If
End Sub
Private Sub TempComboBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 9
Application.ActiveCell.Offset(0, 1).Activate
Case 13
Application.ActiveCell.Offset(1, 0).Activate
End Select
End Sub
ขั้นตอนที่ 6: บันทึกและทดสอบ
- บันทึกโค้ด ปิด Design Mode จากแท็บ Developer
- ตั้งค่า Data Validation ที่เซลล์ C5: เลือก List ใน Allow, เลือกช่วงข้อมูลใน Source
- ลองพิมพ์ตัวอักษรแรกในเซลล์ C5 จะเห็นคำแนะนำปรากฏขึ้น
อ่านเพิ่มเติม: Data Validation Drop Down List with VBA in Excel (7 Applications)
2. Autocomplete ด้วย Combo Box จาก ActiveX Controls (ไม่ใช้ VBA)
ใช้ ActiveX Control เพียงอย่างเดียวสำหรับการตรวจสอบข้อมูลอัตโนมัติ
ขั้นตอนที่ 1-2: แทรก Combo Box และตั้งค่า Properties- แทรก Combo Box จาก ActiveX Control วางที่พื้นที่ว่าง
- คลิกขวาเลือก Properties
- ใน Linked Cell ใส่ C5
- ใน ListFillRange ใส่ $B$5:$B$9
- ใน MatchEntry เลือก 1-fmMatchEntryComplete
- ปิด Design Mode จากแท็บ Developer
- พิมพ์ตัวอักษรใน Combo Box คำแนะนำจะปรากฏ และข้อมูลจะแสดงในเซลล์ C5
อ่านเพิ่มเติม: วิธีสร้าง Excel Drop Down List สำหรับ Data Validation (8 วิธี)
สรุป
บทความนี้แสดง 2 วิธีในการทำ Autocomplete Data Validation drop-down list ใน Excel หวังว่าจะตอบสนองความต้องการของคุณ กรุณาเยี่ยมชมเว็บไซต์ Exceldemy.com และแสดงความคิดเห็นได้ที่กล่องความคิดเห็น
บทความที่เกี่ยวข้อง
- วิธีสร้าง Data Validation List จากตารางใน Excel (3 วิธี)
- สร้าง Data Validation Drop-Down List พร้อมการเลือกหลายรายการใน Excel
- วิธีใช้ Multiple Data Validation ในเซลล์เดียวใน Excel (3 ตัวอย่าง)
- Default Value in Data Validation List with Excel VBA (Macro and UserForm)
- Excel Data Validation Based on Another Cell Value
- Apply Custom Data Validation for Multiple Criteria in Excel (4 Examples)
- Excel Data Validation Alphanumeric Only (Using Custom Formula)