Page 1 of 1

VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 03, 2015 4:08 pm
by Pongwipat
ผมลองใช้ vba ในการค้นข้อมูล โดยติดปัญหาที่เวลาใช้ ชืือ(name) และ นามสกุล(surname) ในการค้นพร้อมแล้ว ไม่สามารถค้นได้

code อยู่ UserForm 1 ที่ปุ่ม ค้น (Search,commandbutton1)

'case 3 name & surname search รบกวนอาจารย์หรือผู้รู้ช่วยหน่อยครับ

Code: Select all

Private Sub CommandButton1_Click()


Dim sat, sat2, s As Integer
Dim deg1, deg2, deg3, deg4 As String

With ListBox1
.Clear
.ColumnCount = 8
.ColumnWidths = "80;80;80;0;0;110;80;0"
End With

deg2 = ComboBox1.Value


'case 1 name search
If ComboBox2.Value = "" Then


For sat = 2 To Sheets("Sheet1").Cells(10000, "b").End(xlUp).Row
Set deg1 = Sheets("Sheet1").Cells(sat, "b")
If UCase(deg1) Like UCase(deg2) & "*" Then
ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat, "H")



s = s + 1

End If: Next

End If


deg3 = ComboBox2.Value

'case 2 surname search
If ComboBox1.Value = "" Then


For sat = 2 To Sheets("Sheet1").Cells(10000, "c").End(xlUp).Row
Set deg1 = Sheets("Sheet1").Cells(sat, "c")
If UCase(deg1) Like UCase(deg3) & "*" Then
ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat, "H")


s = s + 1

End If: Next

End If

'case 3 name & surname search


deg4 = ComboBox1.Value & ComboBox2.Value

If ComboBox1.Value = Value And ComboBox2.Value = Value Then


For sat2 = 2 To Sheets("Sheet1").Cells(10000, "b" & "c").End(xlUp).Row
Set deg1 = Sheets("Sheet1").Cells(sat2, "b" & "c")
If UCase(deg1) Like UCase(deg4) & "*" Then
ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat2, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat2, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat2, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat2, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat2, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat2, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat2, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat2, "H")

s = s + 1

End If: Next

End If


End Sub

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 03, 2015 8:18 pm
by snasui
:D ตัวอย่างสำหรับการค้นหาชื่อและนามสกุลตามด้านล่างครับ

Code: Select all

    'case 3 name & surname search
    deg4 = ComboBox1.Text & ComboBox2.Text
    
    If ComboBox1.Text <> "" And ComboBox2.Text <> "" Then
        For sat2 = 2 To Sheets("Sheet1").Cells(10000, "b").End(xlUp).Row
            deg1 = Sheets("Sheet1").Cells(sat2, "b") & Sheets("Sheet1").Cells(sat2, "c")
            If UCase(deg1) Like UCase(deg4) & "*" Then
                ListBox1.AddItem
                ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat2, "A")
                ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat2, "B")
                ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat2, "C")
                ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat2, "D")
                ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat2, "E")
                ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat2, "F")
                ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat2, "G")
                ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat2, "H")
                s = s + 1
            End If
        Next
    End If

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 03, 2015 10:28 pm
by Pongwipat
ขอบคุณมากครับ

ผมลองทำต่อไปอีก ติดปัญหาตรง การค้นหาระหว่าง สอง ID case สุดท้าย กรณี ID1 < ID2 รบกวนช่วยดูให้หน่อยครับ

Code: Select all

'ID1<ID2

If ComboBox1.Value = "" And ComboBox2.Value = "" And ComboBox3.Value < ComboBox4.Value Then

For sat = 2 To Sheets("Sheet1").Cells(10000, "a").End(xlUp).Row
Set deg7 = Sheets("Sheet1").Cells(sat, "a")

If ComboBox3.Value <= deg7.Value <= ComboBox4.Value & "*" Then


ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat, "H")

s = s + 1

End If: Next

End If

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 03, 2015 11:03 pm
by snasui
:D บรรทัดนี้กำลังจะเขียนว่าอะไรครับ :arrow: If ComboBox3.Value <= deg7.Value <= ComboBox4.Value & "*" Then

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Thu Jun 04, 2015 9:48 am
by Pongwipat

Code: Select all

If ComboBox3.Value <= deg7.Value <= ComboBox4.Value & "*" Then
คือผมต้องการไอดีที่อยู่ระหว่าง ไอดี ที่ใส่ใน combobox3 และ combobox4 ผมให้ deg7.value แทนค่าที่ของไอดีที่อยู่ระหว่าง สองค่าเช่น ผมใส่ 150002 (combobox3) และ 150006(combobox4) เวลาค้นแล้วต้องได้


150002 < =(น้อยกว่าหรือเท่ากับ) 150002,150003,150004,150005,150006 <=(น้อยกว่าหรือเท่ากับ) 150006 ไม่แน่ใจว่าใช้วิธีนี้ได้หรือเปล่าครับ

ยังไงรบกวนอาจารย์แนะนำด้วยครับ

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Thu Jun 04, 2015 10:07 am
by snasui
:D คำว่าอยู่ระหว่าง เขียนแบบด้านล่างครับ

Code: Select all

If ComboBox3.Value <= deg7.Value and deg7.Value <= ComboBox4.Value Then

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 24, 2015 1:36 pm
by Pongwipat
ขออนุญาตต่อจากของเดิมนะครับเพราะคล้าย ๆ กัน ผมลองเขียนโค๊ด ค้นระหว่างสอง ID ได้แล้วและกำลังทำค้นระหว่างสองวันที่ โดยดูตัวอย่างโค๊ดจากของเดิม โดยเปลี่ยน data type จากเดิมเป็น integer เป็น date โค๊ดอยู่ที่ UserForm1 ปุ่ม SEARCH(CommandButton1)

Code: Select all


Dim a As Long
Dim sCrit As String
Dim tCrit, uCrit As Long
Dim vCrit, wCrit As Date
Dim rngSource As Range
Set rngSource = Worksheets("Sheet2").Range("A2:I1000")


'3. between two dates

If Not TextBox1.Value = Empty Then
vCrit = TextBox1.Value
With Me.ListBox1
.ColumnCount = 9
.ColumnWidths = "80;80;80;0;0;110;80;0;30"
.List = rngSource.Cells.Value
For a = .ListCount - 1 To 0 Step -1
If Not UCase(.List(a, 6)) >= vCrit Then
.RemoveItem a
End If
Next a
End With

If Not TextBox1 = Empty Then
wCrit = TextBox2.Value
With Me.ListBox1
For a = .ListCount - 1 To 0 Step -1
If Not UCase(.List(a, 6)) <= wCrit Then
.RemoveItem a
End If
Next a
End With
End If
End If
แต่มันค้นไม่ได้ครับ
อยากถามอาจารย์ว่า date สามารถเปรียบเทียบได้หรือไม่ หรือผิดตรงไหน รบกวนอาจารย์แนะนำด้วยครับ

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 24, 2015 4:22 pm
by snasui
:D ช่วยแจ้งวิธีการทดสอบ Object ใด กรอกค่าใด ผลลัพธ์ที่ถูกต้องควรเป็นอย่างไร จะได้ช่วยทดสอบได้ครับ

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 24, 2015 4:31 pm
by Pongwipat
ครับ ผมค้น DATE OF BIRTH เช่น ผมใส่ วันที่ (1/1/1982) ใน textbox1 และ (1/1/1985) ใน Textbox2 หลังจากนั้น
ใน listbox1 แสดงการค้นคือ

150002 David James B-com b-movie b-state 15/9/1982 15/3/2015
150006 Bandeck Netter BBBBBC a-movie d-state 12/8/1982 19/4/2015

แต่พอผมทดสอบแล้วมันค้นไม่ได้ครับ

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Wed Jun 24, 2015 4:57 pm
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

'3. between two dates

If Not TextBox1.Value = Empty Then
    vCrit = Application.Text(TextBox1.Value, "[$- ]dd/mm/yyyy")
    With Me.ListBox1
        .ColumnCount = 9
        .ColumnWidths = "80;80;80;0;0;110;80;0;30"
        .List = rngSource.Cells.Value
        For a = .ListCount - 1 To 0 Step -1
            If Not Application.Text(.List(a, 6), "[$- ]dd/mm/yyyy") >= vCrit Then
            .RemoveItem a
            End If
        Next a
    End With

    If Not TextBox1 = Empty Then
        wCrit = Application.Text(TextBox2.Value, "[$- ]dd/mm/yyyy")
        With Me.ListBox1
        For a = .ListCount - 1 To 0 Step -1
            If Not Application.Text(.List(a, 6), "[$- ]dd/mm/yyyy") <= wCrit Then
                .RemoveItem a
            End If
        Next a
        End With
    End If
End If

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Mon Jun 29, 2015 8:41 pm
by Pongwipat
พอดีมีปัญหาอีกนิดหนึ่งครับอาจารย์เวลาที่ผมค้นชื่อ ในช่อง Name (combobox1) เวลาที่มีเครื่องหมาย "[]" รวมอยู่ในชื่อผมค้นจาก combobox1 แล้วมันค้นไม่เจอครับ เช่น ชื่อ Peter[JS] code อยู่ในปุ่ม Search commandbutton1 ครับ ผมแนบไฟล์เพิ่มมาด้วย ควรแก้ไขอย่างไรดีครับ

Code: Select all

Private Sub CommandButton1_Click()
Dim a As Long
Dim sCrit As String
Dim tCrit, uCrit As Long
Dim vCrit, wCrit As Date
Dim rngSource As Range
Set rngSource = Worksheets("Sheet2").Range("A2:I1000")
 
 '1.name
 If Not ComboBox1.Value = Empty Then
sCrit = "*" & UCase(Me.ComboBox1) & "*"
With Me.ListBox1
.ColumnCount = 9
.ColumnWidths = "80;80;80;0;0;110;80;0;30"
.List = rngSource.Cells.Value
For a = .ListCount - 1 To 0 Step -1
If Not UCase(.List(a, 1)) Like sCrit Then
.RemoveItem a
End If
Next a
End With
End If

Re: VBA Form ถามเรื่องการค้นข้อมูลครับ

Posted: Mon Jun 29, 2015 10:26 pm
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

If Not ComboBox1.Value = Empty Then
    sCrit = UCase(Me.ComboBox1)
    With Me.ListBox1
        .ColumnCount = 9
        .ColumnWidths = "80;80;80;0;0;110;80;0;30"
        .List = rngSource.Cells.Value
        For a = .ListCount - 1 To 0 Step -1
            If sCrit <> UCase(.List(a, 1)) Then
                .RemoveItem a
            End If
        Next a
    End With
End If