Re: แสดงข้อมูลใน Listbox
Posted: Thu Nov 16, 2017 6:02 pm
เข้าใจค่ะ แต่ที่ต้องการคือข้อมูลในชีทการเบิกคือชีท9 ค่ะ ไม่ใช้ข้อมูลในชีท6
คลังคำตอบแห่งความรู้จากคนไทย เพื่อโลกที่ต้องการเข้าใจในสิ่งเล็ก ๆ อย่างลึกซึ้ง
http://www.snasui.com/
Benmore wrote:กระทู้นี้เป็นกระทู้ที่ต้องการให้ค้นหาข้อมูลการเบิกด้วยวันที่ กับ รหัสค่ะ
ตัวอย่าง![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
ชีทการเบิก(ชีท9)
รหัส 009 |นาย ก |วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 010 |นาย ข |วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 009 |นาย ก |วันที่ 17 srptember 2017 | เบิก เสื้อ 2 ตัว กางเกง 2 ตัว
รหัส 009 |นาย ก |วันที่ 18 srptember 2017 | เบิก เสื้อ 3 ตัว กางเกง 3 ตัว
![]()
Userform1
เมื่อใส่วันที่ 16 srptember 2017 แล้วกดปุ่มค้นหาก็จะแสดงข้อมูลใน listbox
รหัส 009 |นาย ก |เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 010 |นาย ข |เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
เมื่อใส่รหัส 009 แล้วกดปุ่มค้นหาก็จะแสดงข้อมูลใน listbox
วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
วันที่ 17 srptember 2017 | เบิก เสื้อ 2 ตัว กางเกง 2 ตัว
วันที่ 18 srptember 2017 | เบิก เสื้อ 3 ตัว กางเกง 3 ตัว
แสดงเป็นตารางเพื่อแก้ไขข้อมูลในขั้นตอนถัดไปได้ค่ะ
Benmore wrote:แก้ไขได้ประมาณนี้ค่ะ แต่ก็ยังรันไม่ได้ค่ะ ข้อมูลที่ค้นหาด้วยรหัสก็ไม่แสดงใน listbox เลยค่ะCode: Select all
Private Sub btsearch1_Click() Dim found As Boolean Dim txt As String Dim r As Range Dim chkDate As Date On Error Resume Next Dim nRow As String For Each r In Sheet6.Columns(1).SpecialCells(xlCellTypeConstants) Sheet6.Activate chkDate = CDate(Applicion.Text(cmday & "/" & cmmonth & "/" & cmyear, "ddmmyyyy")) If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value = chkDate Then nRow = r.row found = True Exit For End If Next r If found Then If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then MsgBox "กรุณาใส่ข้อมูลเป็นตัวเลข" Exit Sub End If If Err.Number = 91 Then TextBox1.RowSource = "txtsearch1.Text" TextBox7.Value = "" TextBox8.Value = "" TextBox9.Value = "" TextBox10.Value = "" End If
Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
#24 เป็นการค้นหาข้อมูลค่ะ ไม่ใช่การบันทึกข้อมูล ให้ค้นหาข้อมูลในชีท 9 แล้วมาแสดงบน listbox ค่ะ ไม่ได้มีคำถามเกี่ยวกับการบันทึกแต่อย่างใด มีแค่การแสดงข้อมูลใน Listbox ค่ะsnasui wrote:Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะเมื่อต้องการบันทึกไปยัง Sheet9 ก็ต้องเขียน Code ให้บันทึกไปยัง Sheet9 ติดปัญหาแล้วเอามาถามกัน ใน Code ข้างต้นนี้เขียนเกียวกับ Sheet6 เกี่ยวกับการ Search สอดคล้องกับหัวกระทู้ที่เขียนว่า "แสดงข้อมูลใน Listbox" ไม่เกียวกับการบันทึก ตามที่ยกมาให้ดูในโพสต์ #24
ไม่เกี่ยวกับ Sheet9 แต่อย่างใด กรุณาทบทวนดูใหม่ครับ
Code: Select all
Dim found As Boolean
Dim txt As String
Dim r As Range
Dim chkDate As Date
On Error Resume Next
Dim nRow As String
chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
Sheet9.Activate
For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
End If
Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
Code: Select all
'ค้นหา 1
Private Sub btsearch1_Click()
'On Error Resume Next
Dim found As Boolean
Dim txt As String
Dim r As Range
Dim chkDate As Date
' On Error Resume Next
Dim nRow As String
chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
Sheet9.Activate
For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
' If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
' MsgBox "กรุณาใส่ข้อมูลเป็นตัวเลข"
' Exit Sub
' End If
If Err.Number = 91 Then
TextBox1.RowSource = "txtsearch1.Text"
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
'MsgBox "ไม่มีข้อมูล"
End If
TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 2)
TextBox9.Value = Cells(nRow, 3)
TextBox10.Value = Cells(nRow, 4)
txt = "Emp_ID : " & Cells(nRow, 1) & vbCrLf & _
"Name : " & Cells(nRow, 2) & vbCrLf & _
"Section : " & Cells(nRow, 3) & vbCrLf & _
"Uniform_No : " & Cells(nRow, 4) & vbCrLf & _
"Date : " & Cells(nRow, 5) & vbCrLf & _
"Discription : " & Cells(nRow, 6) & vbCrLf & _
"Reason : " & Cells(nRow, 7)
ListBox1.AddItem txt
Exit Sub
Else
MsgBox "ไม่มีข้อมูล"
End If
Sheet1.Activate
End Sub
VbCrlf
เหมือนเช่น TextBox ที่สามารถกำหนด Multiline ช่วยเพื่อแสดงข้อความที่คั่นด้วย VbCrlf
เป็นหลายบรรทัดครับต้องแก้ไขโค้ดตรงไหนค่ะBenmore wrote:snasui wrote:Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะเมื่อปัญหาคือการ Search ก็ไม่ควรกล่าวถึงการบันทึก แยกประเด็นกันให้ชัด การสอบถามควรมุ่งไปที่การอธิบายปัญหาที่เกิดจากการ Search เป็นหลักครับ
สรุปปัญหาคือต้องการค้นหาแล้วให้ข้อมูลแสดงใน ListBox ตัวอย่างการปรับ Code คือด้านล่าง
โปรดสังเกตว่ามีการ Mark บรรทัดใดไปเป็น Comment บ้าง หากจะใช้จะต้องพิจารณาบรรทัดนั้นใหม่ อีกประการ ListBox ไม่สามารกแยกแสดงเป็นหลายบรรทัดด้วยการใช้Code: Select all
แล้วต้องแก้ไขโค้ดตรงไหนเพิ่มค่ะ 'ค้นหา 1 Private Sub btsearch1_Click() 'On Error Resume Next Dim found As Boolean Dim txt As String Dim r As Range Dim chkDate As Date ' On Error Resume Next Dim nRow As String chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday) Sheet9.Activate For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants) If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then nRow = r.Row found = True Exit For End If Next r If found Then ' If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then ' MsgBox "กรุณาใส่ข้อมูลเป็นตัวเลข" ' Exit Sub ' End If If Err.Number = 91 Then TextBox1.RowSource = "txtsearch1.Text" TextBox7.Value = "" TextBox8.Value = "" TextBox9.Value = "" TextBox10.Value = "" 'MsgBox "ไม่มีข้อมูล" End If TextBox7.Value = Cells(nRow, 1) TextBox8.Value = Cells(nRow, 2) TextBox9.Value = Cells(nRow, 3) TextBox10.Value = Cells(nRow, 4) txt = "Emp_ID : " & Cells(nRow, 1) & vbCrLf & _ "Name : " & Cells(nRow, 2) & vbCrLf & _ "Section : " & Cells(nRow, 3) & vbCrLf & _ "Uniform_No : " & Cells(nRow, 4) & vbCrLf & _ "Date : " & Cells(nRow, 5) & vbCrLf & _ "Discription : " & Cells(nRow, 6) & vbCrLf & _ "Reason : " & Cells(nRow, 7) ListBox1.AddItem txt Exit Sub Else MsgBox "ไม่มีข้อมูล" End If Sheet1.Activate End Sub
VbCrlf
เหมือนเช่น TextBox ที่สามารถกำหนด Multiline ช่วยเพื่อแสดงข้อความที่คั่นด้วยVbCrlf
เป็นหลายบรรทัดครับ
Benmore wrote:ต้องแก้ไขโค้ดตรงไหนค่ะ
Code: Select all
Private Sub btsearch1_Click()
On Error Resume Next
If txtsearch1.Value = "" Or cmday.Value = "" Or cmmonth.Value = "" Or cmyear.Value = "" Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅãËé¤Ãº"
Combobox1.Value = ""
Exit Sub
End If
Dim found As Boolean
Dim txt As String
Dim r As Range
Dim chkDate As Date
Dim nRow As String
chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
Sheet9.Activate
For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
If Right(r.Value, 3) = Right(txtsearch1.Value, 3) Or r.Offset(0, 0).Value2 = CLng(chkDate) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
End If
If Err.Number = 91 Then
'TextBox1.RowSource = "txtsearch1.Text"
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
TextBox11.Value = ""
TextBox16.Value = ""
TextBox17.Value = ""
TextBox12.Value = ""
TextBox13.Value = ""
'MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 2)
TextBox9.Value = Cells(nRow, 3)
TextBox10.Value = Cells(nRow, 4)
TextBox11.Value = Cells(nRow, 6)
TextBox16.Value = Cells(nRow, 7)
TextBox17.Value = Cells(nRow, 8)
TextBox12.Value = Cells(nRow, 9)
TextBox13.Value = Cells(nRow, 10)
Exit Sub
Else
MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
Sheet1.Activate
End Sub
Code: Select all
'Other code
If Right(r.Value, 3) = Right(txtsearch1.Value, 3) And r.Offset(0, 4).Value2 = CLng(chkDate) Then
nRow = r.Row
found = True
Exit For
End If
'Other code
ตรงนี้ทำได้แล้วขอบคุณมากค่ะ รบกวนดูตรง OptionButton ให้หน่อยค่ะsnasui wrote:ตัวอย่างการค้นหา 2 เงื่อนไขพร้อมกันครับ
Code: Select all
'Other code If Right(r.Value, 3) = Right(txtsearch1.Value, 3) And r.Offset(0, 4).Value2 = CLng(chkDate) Then nRow = r.Row found = True Exit For End If 'Other code
Code: Select all
Private Sub btsearch1_Click()
On Error Resume Next
If txtsearch1.Value = "" Or cmday.Value = "" Or cmmonth.Value = "" Or cmyear.Value = "" Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅãËé¤Ãº"
Combobox1.Value = ""
Exit Sub
End If
Dim found As Boolean
Dim txt As String
Dim r As Range
Dim chkDate As Date
Dim nRow As String
chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
Sheet9.Activate
For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
If Right(r.Value, 3) = Right(txtsearch1.Value, 3) Or r.Offset(0, 0).Value2 = CLng(chkDate) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
TextBox11.Value = ""
TextBox16.Value = ""
TextBox17.Value = ""
TextBox12.Value = ""
If OptionButton9.Value = True Then
ElseIf OptionButton10.Value = True Then
ElseIf OptionButton11.Value = True Then
OptionButton11.Value = TextBox13.Value
End If
'MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 2)
TextBox9.Value = Cells(nRow, 3)
TextBox10.Value = Cells(nRow, 4)
TextBox11.Value = Cells(nRow, 6)
TextBox16.Value = Cells(nRow, 7)
TextBox17.Value = Cells(nRow, 8)
TextBox12.Value = Cells(nRow, 9)
If OptionButton9.Value = True Then
OptionButton9.Value = Cells(nRow, 10)
ElseIf OptionButton10.Value = True Then
OptionButton10.Value = Cells(nRow, 10)
ElseIf OptionButton11.Value = True Then
TextBox13.Value = Cells(nRow, 10)
End If
Exit Sub
Else
MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
Sheet1.Activate
End Sub
Benmore wrote:![]()
![]()
![]()
![]()
![]()
Code: Select all
Private Sub CommandButton5_Click()
If txtsearch.Value = "" Or Commonth.Value = "" Then
MsgBox "¡ÃسÒàÅ×Í¡¢éÍÁÙÅ¡è͹"
Exit Sub
End If
Dim found As Boolean
Dim txt As String
Dim r As Range
On Error Resume Next
Dim nRow As String
For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
Sheet9.Activate
If Right(r.Value, 3) = Right(txtsearch.Text, 3) Then
nRow = r.Row
found = True
Exit For
End If
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch.Text, 3)) Then
MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
Exit Sub
End If
If Err.Number = 91 Then
TextBox1.RowSource = "txtsearch.Text & combobox1.value"
End If
txt = "Emp_ID : " & Cells(nRow, 1) & vbCrLf & _
"Name : " & Cells(nRow, 2) & vbCrLf & _
"Section : " & Cells(nRow, 3) & vbCrLf & _
"Uniform_No : " & Cells(nRow, 4) & vbCrLf & vbCrLf & _
"Date : " & Cells(nRow, 5) & vbCrLf & _
"Discription : " & Cells(nRow, 6) & vbCrLf & _
"Reason : " & Cells(nRow, 7) & vbCrLf & _
"Status : " & Cells(nRow, 8)
TextBox1.Value = txt
Exit Sub
Else
MsgBox "äÁèÁÕ¢éÍÁÙÅ"
End If
End Sub
Code: Select all
'Other code
If Right(r.Value, 3) = Right(txtsearch.Text, 3) And _
Commonth.Text = Application.Text(r.Offset(0, 4).Value, "[$- ]mmmm") Then
nRow = r.Row
found = True
Exit For
End If
'Other code