: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 Form ถามเรื่องการค้นข้อมูลครับ

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 Form ถามเรื่องการค้นข้อมูลครับ

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

#12

by snasui » Mon Jun 29, 2015 10:26 pm

: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

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

#11

by Pongwipat » Mon Jun 29, 2015 8:41 pm

พอดีมีปัญหาอีกนิดหนึ่งครับอาจารย์เวลาที่ผมค้นชื่อ ในช่อง 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
Attachments
Searchtest5(name).xlsm
แนบไฟล์
(42.05 KiB) Downloaded 33 times

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

#10

by snasui » Wed Jun 24, 2015 4:57 pm

: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 ถามเรื่องการค้นข้อมูลครับ

#9

by Pongwipat » Wed Jun 24, 2015 4:31 pm

ครับ ผมค้น 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 ถามเรื่องการค้นข้อมูลครับ

#8

by snasui » Wed Jun 24, 2015 4:22 pm

:D ช่วยแจ้งวิธีการทดสอบ Object ใด กรอกค่าใด ผลลัพธ์ที่ถูกต้องควรเป็นอย่างไร จะได้ช่วยทดสอบได้ครับ

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

#7

by Pongwipat » Wed Jun 24, 2015 1:36 pm

ขออนุญาตต่อจากของเดิมนะครับเพราะคล้าย ๆ กัน ผมลองเขียนโค๊ด ค้นระหว่างสอง 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 สามารถเปรียบเทียบได้หรือไม่ หรือผิดตรงไหน รบกวนอาจารย์แนะนำด้วยครับ
Attachments
Searchtest5.xlsm
แนบไฟล์
(41.58 KiB) Downloaded 15 times

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

#6

by snasui » Thu Jun 04, 2015 10:07 am

:D คำว่าอยู่ระหว่าง เขียนแบบด้านล่างครับ

Code: Select all

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

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

#5

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

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 ถามเรื่องการค้นข้อมูลครับ

#4

by snasui » Wed Jun 03, 2015 11:03 pm

:D บรรทัดนี้กำลังจะเขียนว่าอะไรครับ :arrow: If ComboBox3.Value <= deg7.Value <= ComboBox4.Value & "*" Then

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

#3

by Pongwipat » Wed Jun 03, 2015 10:28 pm

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

ผมลองทำต่อไปอีก ติดปัญหาตรง การค้นหาระหว่าง สอง 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
Attachments
Searchtest2.xlsm
แนบไฟล์
(30.72 KiB) Downloaded 16 times

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

#2

by snasui » Wed Jun 03, 2015 8:18 pm

: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

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

#1

by Pongwipat » Wed Jun 03, 2015 4:08 pm

ผมลองใช้ 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
Attachments
Searchtest.xlsm
แนบไฟล์
(31.34 KiB) Downloaded 28 times

Top