: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

มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ 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. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :thup: :cp: :flw: :rz: :sg: :tt: :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :ard: :arl: :aru: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

Re: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#7

by boylee1928 » Tue Oct 29, 2019 1:26 pm

รับทราบครับขอบคุณมากครับ

Re: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#6

by snasui » Sat Oct 26, 2019 8:40 pm

:D ถ้าให้ผ่านการกรองด้วยผมได้ตอบไปแล้วตามโพสต์ #2 ถ้าไม่ต้องผ่านการกรองก็ใช้ TextChange ของแต่ละ Object ตามที่เขียนมา ไม่ทราบติดปัญหาตรงไหนครับ

Re: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#5

by boylee1928 » Sat Oct 26, 2019 8:12 pm

ตอนเเรกผมพิมพ์ใส่textbox2ไปก็จะขึ้นโชว์ค่าที่พิมพ์ไป
test1.JPG
test1.JPG (29.6 KiB) Viewed 54 times
แต่เมื่อผมพิมพ์ใส่textbox3ก็จะขึ้นโชว์ค่าที่Textbox3โดยไม่ได้ผ่านการกรองของtextbox2มาครับ
test2.JPG
test2.JPG (31 KiB) Viewed 54 times
คล้ายๆกับการใช้ฟังก์ชันFilterครับที่ผมต้องการ

Re: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#4

by snasui » Fri Oct 25, 2019 9:20 pm

:D ช่วยขยายความคำว่ากรองแบบ Text ต่อ Text พร้อมยกตัวอย่างมาเพิ่มเติมสัก 2-3 ตัวอย่าง จะได้เห็นว่าคีย์อย่างไร คำตอบเป็นอย่างไรครับ

Re: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#3

by boylee1928 » Fri Oct 25, 2019 9:05 am

ขอบคุณมากครับเเต่ผมอยากได้เเนวประมาณการกรองเเบบTextต่อTextเลยครับ

Re: มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#2

by snasui » Thu Oct 24, 2019 8:53 pm

:D ควรจะให้ Search ทีเดียวเมื่อคีย์ครบแล้ว การให้คีย์ไป Search ไปจะต้องมีตัวแปรมารับค่าจาก TextBox เทียบกับค่าในเซลล์หรือตัวแปรที่แทนค่าในเซลล์ นอกจากนี้ยังไม่เหมาะที่จะใช้ Event TextChange เพราะ Code จะทำงานทุกอักขระที่คีย์

หากเลือกเป็นการ Search ทุกครั้งที่คีย์ใน TextBox ในส่วนของตัวแปรเช่นตั้งเป็น strTextSeach แล้วตรวจสอบว่า TextBox ใดมีค่าไม่ว่างบ้างให้นำค่าใน TextBox นั้นมาเชื่อมกันเช่นนี้เป็นต้น

ตัวอย่าง Code ซึ่งจะต้องปรับให้ตรงกับงานจริง Code นี้จะทำงานเมื่อกรอก TextBox1 แล้ว Enter ครับ

Code: Select all

Private Sub TextBox1_AfterUpdate()
    Dim i As Long
    Dim strRange As String
    Dim strTextSearch As String
    Dim a() As Variant
    Dim k As Integer
    Dim j As Integer
    Dim rall As Range
    Dim r As Range
    
    ListBox1.Clear
    If TextBox1.Text <> "" Then
        strTextSearch = TextBox1.Text
        ReDim Preserve a(k)
        a(k) = 1
        k = k + 1
    End If
    If TextBox2.Text <> "" Then
        strTextSearch = strTextSearch & TextBox2.Text
        ReDim Preserve a(k)
        a(k) = 2
        k = k + 1
    End If
    If TextBox3.Text <> "" Then
        strTextSearch = strTextSearch & TextBox3.Text
        ReDim Preserve a(k)
        a(k) = 3
        k = k + 1
    End If
    'Other remaining Textbox code
    For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
        For j = 0 To UBound(a)
            strRange = strRange & Range("a" & i).Offset(0, j).Value
        Next j
        If strTextSearch = strRange Then
            With Me.ListBox1
                .AddItem Sheet1.Cells(i, 1).Value
                .List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
                .List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
                .List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
                .List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
                .List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
                .List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
            End With
        End If
    Next i
End Sub

มีปัญาเกี่ยวกับการกรองข้อมูลจากการหาข้อมูลโดยใช้ VBA

#1

by boylee1928 » Thu Oct 24, 2019 3:42 pm

ผมต้องการที่จะกรองข้อมูลจากTextbox หลายๆTextbox เเล้วเเสดงใน Listbox พอจะมีเเนวทางการเขียนโค๊ดไหมครับ
ตัวอย่างโค๊ดที่ผมเขียนสามารถกรองได้เเค่Textboxเดียวเท่านั้น

Code: Select all

Private Sub TextBox1_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox1.Text)
If StrConv(Left(Sheet1.Cells(i, 1).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox1.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i
End Sub

Private Sub TextBox2_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox2.Text)
If StrConv(Left(Sheet1.Cells(i, 2).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox2.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i

End Sub

Private Sub TextBox3_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox3.Text)
If StrConv(Left(Sheet1.Cells(i, 3).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox3.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i
End Sub

Private Sub TextBox4_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox4.Text)
If StrConv(Left(Sheet1.Cells(i, 4).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox4.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i
End Sub

Private Sub TextBox5_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox5.Text)
If StrConv(Left(Sheet1.Cells(i, 5).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox5.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i
End Sub

Private Sub TextBox6_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox6.Text)
If StrConv(Left(Sheet1.Cells(i, 6).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox6.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i
End Sub

Private Sub TextBox7_Change()
Dim i As Long
ListBox1.Clear
Me.ListBox1.AddItem
   For X = 1 To 7
     Me.ListBox1.List(0, X - 1) = Sheet1.Cells(1, X)
   Next X
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
a = Len(Me.TextBox7.Text)
If StrConv(Left(Sheet1.Cells(i, 7).Value, a), vbLowerCase) = StrConv(Left(Me.TextBox7.Text, a), vbLowerCase) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 3).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = Sheet1.Cells(i, 7).Value
End If
Next i

End Sub
ไฟล์ตัวอย่างครับ
Book1.xlsm
(22.08 KiB) Downloaded 10 times

ตัวอย่างรูปภาพครับ
1.JPG
1.JPG (31.41 KiB) Viewed 86 times

Top