snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ผมสร้างฟอร์มเพื่อค้นหาจาก id แล้วแสดงข้อมูลที่ค้นหาพบ แต่ไม่สามารถค้นหาได้ทุก id ถึงแม้จะมีรายการอยู่ โดยตัวอักษรสีแดงจะไม่สามารถค้นหาเจอ รบกวนแนะนำวิธีแก้ไขด้วยครับ ขอบพระคุณครับ
Private Sub CommandButton1_Click()
Dim RecordRow As Long
Dim RecordRange As Range
On Error Resume Next
RecordRow = Application.Match(CLng(TextBox1.Value), Range("Table1[id]"), 0)
Set RecordRange = Range("Table1").Cells(1, 1).Offset(RecordRow - 1, 0)
If Err.Number <> 0 Then
ErrorLabel.Visible = True
On Error GoTo 0
Exit Sub
End If
On Error GoTo 0
ErrorLabel.Visible = False
TextBox2.Value = RecordRange(1, 1).Offset(0, 1).Value
End Sub
You do not have the required permissions to view the files attached to this post.
คอลัมน์ ID มีทั้ง Data Type ที่เป็น Number และ Text การค้นหาจะต้องค้นหาทั้งสองรูปแบบ ปกติควรกำหนด Data Type ของคอลัมน์ใด ๆ ให้เป็นรูปแบบเดียวเท่านั้น การทำงานกับข้อมูลจะต้องคำนึงถึงสิ่งนี้เป็นสำคัญครับ
ตัวอย่างการปรับ Code ให้หาได้ทั้ง Text และ Number (ซึ่งไม่แนะนำ)
Private Sub CommandButton1_Click()
Dim RecordRow As Long, mText As Long, mNum As Long
Dim RecordRange As Range
ErrorLabel.Visible = False
On Error Resume Next
mText = Application.Match(TextBox1.Value, Range("Table1[id]"), 0)
mNum = Application.Match(CLng(TextBox1.Value), Range("Table1[id]"), 0)
RecordRow = Application.Max(mText, mNum)
TextBox2.Text = ActiveSheet.Cells(RecordRow, "b").Value
If RecordRow = 0 Then
ErrorLabel.Visible = True
End If
' Set RecordRange = Range("Table1").Cells(1, 1).Offset(RecordRow - 1, 0)
' If Err.Number <> 0 Then
'
' ErrorLabel.Visible = True
' On Error GoTo 0
' Exit Sub
'
' End If
'
On Error GoTo 0
'ErrorLabel.Visible = False
' TextBox2.Value = RecordRange(1, 1).Offset(0, 1).Value
End Sub
snasui wrote: Wed Oct 11, 2023 5:50 am คอลัมน์ ID มีทั้ง Data Type ที่เป็น Number และ Text การค้นหาจะต้องค้นหาทั้งสองรูปแบบ ปกติควรกำหนด Data Type ของคอลัมน์ใด ๆ ให้เป็นรูปแบบเดียวเท่านั้น การทำงานกับข้อมูลจะต้องคำนึงถึงสิ่งนี้เป็นสำคัญครับ
ตัวอย่างการปรับ Code ให้หาได้ทั้ง Text และ Number (ซึ่งไม่แนะนำ)
Private Sub CommandButton1_Click()
Dim RecordRow As Long, mText As Long, mNum As Long
Dim RecordRange As Range
ErrorLabel.Visible = False
On Error Resume Next
mText = Application.Match(TextBox1.Value, Range("Table1[id]"), 0)
mNum = Application.Match(CLng(TextBox1.Value), Range("Table1[id]"), 0)
RecordRow = Application.Max(mText, mNum)
TextBox2.Text = ActiveSheet.Cells(RecordRow, "b").Value
If RecordRow = 0 Then
ErrorLabel.Visible = True
End If
' Set RecordRange = Range("Table1").Cells(1, 1).Offset(RecordRow - 1, 0)
' If Err.Number <> 0 Then
'
' ErrorLabel.Visible = True
' On Error GoTo 0
' Exit Sub
'
' End If
'
On Error GoTo 0
'ErrorLabel.Visible = False
' TextBox2.Value = RecordRange(1, 1).Offset(0, 1).Value
End Sub