:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

แสดงข้อมูลใน Listbox

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

Re: แสดงข้อมูลใน Listbox

#41

Post by Benmore »

snasui wrote::D ตัวอย่าง 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
ค้นหาได้แล้วค่ะแต่ทำไมถึงแสดงแค่ข้อมูลเดียว ไม่แสดงทั้งหมดค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30750
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: แสดงข้อมูลใน Listbox

#42

Post by snasui »

:D Code นี้เป็นตัวอย่างการกำหนดเงื่อนไขเพื่อให้หาบรรทัดที่พบรายการตามเงื่อนไขที่กำหนด หากต้องการให้แสดงทุกรายการในชีต "การเบิก" ก็ต้อง Loop ทุกรายการในชีต "การเบิก" ว่าตรงตามเงื่อนไขนี้หรือไม่ ถ้าตรงก็ให้แสดง เช่นนี้เป็นต้น จำเป็นต้องเขียนเพิ่มเติมมาเองก่อน ติดแล้วค่อยถามกันต่อครับ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

Re: แสดงข้อมูลใน Listbox

#43

Post by Benmore »

พอมีตัวอย่างโค้ดไหมค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30750
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: แสดงข้อมูลใน Listbox

#44

Post by snasui »

:D ตัวอย่างการ Loop เพื่อให้เข้าถึง Range หรือช่วงข้อมูลพบได้จำนวนมากใน Forum นี้ครับ ตัวอย่างเช่น For Each...Next, viewtopic.php?f=3&t=11022#p68518
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

Re: แสดงข้อมูลใน Listbox

#45

Post 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
ได้ประมาณนี่แต่ก็ยังเหมือนเดิมค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30750
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: แสดงข้อมูลใน Listbox

#46

Post by snasui »

:D กรุณาแนบไฟล์ล่าสุดพร้อม Code ล่าสุดและส่วนที่เกียวข้อง ส่วนที่ไม่เกี่ยวข้องกรุณาลบทิ้งไปก่อนจะได้สะดวกในการตอบครับ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

Re: แสดงข้อมูลใน Listbox

#47

Post by Benmore »

Copy of Uniform.xlsm
ไฟล์แนบค่ะ :D
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30750
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: แสดงข้อมูลใน Listbox

#48

Post by snasui »

:D ตัวอย่าง 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 ถัด ๆ ไป ค่อย ๆ เรียนรู้ไปครับ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

Re: แสดงข้อมูลใน Listbox

#49

Post by Benmore »

ตรงนี้ทำได้แล้วค่ะ แต่ถ้าอยากให้กดได้แค่ครั้งเดียวหรือต่อให้กดหลายๆครั้งก็จะไม่แสดงข้อมูลเดิมซ้ำอีกต้องแกตรงไหนค่ะ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30750
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: แสดงข้อมูลใน Listbox

#50

Post by snasui »

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