Page 3 of 3
Re: แสดงข้อมูลใน Listbox
Posted: Wed Nov 29, 2017 8:49 am
by Benmore
snasui wrote:
ตัวอย่าง Code ครับ
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
ค้นหาได้แล้วค่ะแต่ทำไมถึงแสดงแค่ข้อมูลเดียว ไม่แสดงทั้งหมดค่ะ
Re: แสดงข้อมูลใน Listbox
Posted: Fri Dec 01, 2017 6:27 am
by snasui

Code นี้เป็นตัวอย่างการกำหนดเงื่อนไขเพื่อให้หาบรรทัดที่พบรายการตามเงื่อนไขที่กำหนด หากต้องการให้แสดงทุกรายการในชีต "การเบิก" ก็ต้อง Loop ทุกรายการในชีต "การเบิก" ว่าตรงตามเงื่อนไขนี้หรือไม่ ถ้าตรงก็ให้แสดง เช่นนี้เป็นต้น จำเป็นต้องเขียนเพิ่มเติมมาเองก่อน ติดแล้วค่อยถามกันต่อครับ
Re: แสดงข้อมูลใน Listbox
Posted: Fri Dec 01, 2017 2:26 pm
by Benmore
พอมีตัวอย่างโค้ดไหมค่ะ
Re: แสดงข้อมูลใน Listbox
Posted: Sun Dec 03, 2017 4:58 pm
by snasui

ตัวอย่างการ Loop เพื่อให้เข้าถึง Range หรือช่วงข้อมูลพบได้จำนวนมากใน Forum นี้ครับ ตัวอย่างเช่น
For Each...Next,
viewtopic.php?f=3&t=11022#p68518
Re: แสดงข้อมูลใน Listbox
Posted: Thu Dec 07, 2017 10:29 am
by Benmore
Code: Select all
If txtsearch.Text = "" Or Commonth.Text = "" Then
MsgBox "Please fill the complete data"
Exit Sub
End If
Dim found As Boolean
Dim txt As String
Dim r As Range
Dim nRow As String
Dim rall As Range
'For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
Sheet9.Activate
For Each r In Worksheets("การเบิก").Range("A1:A10000").Cells
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
Next r
If found Then
If Not IsNumeric(VBA.Right(txtsearch.Text, 3)) Then
MsgBox "Please enter only numeric data"
Exit Sub
End If
'If Err.Number = 91 Then
'TextBox1.RowSource = "txtsearch.Text & combobox1.value"
' End If
With Sheets("การเบิก")
Set r = .Rows(1).Find(txt)
Set rall = .Range(r.Offset(1, 0), .Cells(.Rows.Count, r.Column).End(xlUp)) _
.SpecialCells(xlCellTypeConstants)
End With
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 "Data not found !"
End If
ได้ประมาณนี่แต่ก็ยังเหมือนเดิมค่ะ
Re: แสดงข้อมูลใน Listbox
Posted: Sat Dec 09, 2017 11:49 am
by snasui

กรุณาแนบไฟล์ล่าสุดพร้อม Code ล่าสุดและส่วนที่เกียวข้อง ส่วนที่ไม่เกี่ยวข้องกรุณาลบทิ้งไปก่อนจะได้สะดวกในการตอบครับ
Re: แสดงข้อมูลใน Listbox
Posted: Sat Dec 09, 2017 12:37 pm
by Benmore
Copy of Uniform.xlsm
ไฟล์แนบค่ะ

Re: แสดงข้อมูลใน Listbox
Posted: Sat Dec 09, 2017 2:33 pm
by snasui

ตัวอย่าง Code ครับ
Code: Select all
Dim txt As String, r As Range
Dim nRow As String, rall As Range, i As Integer
If txtsearch.Text = "" Or Commonth.Text = "" Then
MsgBox "Please fill the complete data"
Exit Sub
End If
If Not IsNumeric(VBA.Right(txtsearch.Text, 3)) Then
MsgBox "Please enter only numeric data"
Exit Sub
End If
With Sheets("การเบิก")
Set rall = .Range("a3", .Range("a" & .Rows.Count).End(xlUp)) _
.SpecialCells(xlCellTypeConstants)
End With
txt = ""
For Each r In rall
If Right(r.Value, 3) = Right(txtsearch.Text, 3) And _
Commonth.Text = Application.Text(r.Offset(0, 4).Value, "[$- ]mmmm") Then
nRow = r.Row
i = i + 1
txt = "รายการที่ " & i & vbCrLf & _
"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) & vbCrLf & _
"Status : " & Cells(nRow, 8)
TextBox1.Value = IIf(TextBox1.Value <> "", TextBox1.Value & vbCrLf & vbCrLf & txt, txt)
End If
Next r
If Len(txt) = 0 Then
MsgBox "Data not found!"
End If
กรุณาสังเกตการประกาศตัวแปร การเรียงลำดับ Code การเยื้อง Code ให้อ่านง่ายทั้งตัวเองและผู้อื่น
Code นี้ผมตั้งใจให้ใช้ดูเป็นตัวอย่างในการเขียน Code ถัด ๆ ไป ค่อย ๆ เรียนรู้ไปครับ
Re: แสดงข้อมูลใน Listbox
Posted: Sat Dec 09, 2017 3:31 pm
by Benmore
ตรงนี้ทำได้แล้วค่ะ แต่ถ้าอยากให้กดได้แค่ครั้งเดียวหรือต่อให้กดหลายๆครั้งก็จะไม่แสดงข้อมูลเดิมซ้ำอีกต้องแกตรงไหนค่ะ
Re: แสดงข้อมูลใน Listbox
Posted: Sat Dec 09, 2017 3:45 pm
by snasui

ต้องออกแบบและเขียนมาเอง ติดแล้วค่อยถามกันเหมือนเช่นที่ผ่านมาครับ