snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
สวัสดีครับอาจารย์ผมมีปัญหาสอบถามอาจารย์เรื่องการสอบถามข้อมูลลูกค้าในชีทงาน..
คีย์เงื่อนไขแล้วข้อมูลไม่แสดงข้อมูลครับ..
Option Explicit
Private Sub CommandButton1_Click()
Dim customer_data As String
Dim data_row As Single
Dim i As Single
Const db_sheet = "db_customers"
Const id_col = 1
Const sub_col = 2
Const dept_col = 3
Const name_col = 4
Const type_col = 5
Const account1_col = 6
Const account2_col = 7
Const account3_col = 8
Const account4_col = 9
Const account5_col = 10
Const account6_col = 11
With Worksheets(db_sheet)
data_row = 2
'set matching data
If TextBox1.Text = Worksheets(db_sheet).Cells(data_row, id_col).Value And TextBox2.Text = Worksheets(db_sheet).Cells(data_row, sub_col).Value And TextBox3.Text = Worksheets(db_sheet).Cells(data_row, dept_col).Value Then
data_row = data_row + 2
customer_data = "True"
End If
End With
'if id ,sub and dept match, data show
If customer_data = "True" Then
'other code
'set matching data
Do While .Cells(data_row, id_col) <> ""
If TextBox1.Text = Worksheets(db_sheet).Cells(data_row, id_col).Value And TextBox2.Text = Worksheets(db_sheet).Cells(data_row, sub_col).Value And TextBox3.Text = Worksheets(db_sheet).Cells(data_row, dept_col).Value Then
customer_data = "True"
Exit Do
End If
data_row = data_row + 1
Loop
'other code