: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

#21

Post by Benmore »

เข้าใจค่ะ แต่ที่ต้องการคือข้อมูลในชีทการเบิกคือชีท9 ค่ะ ไม่ใช้ข้อมูลในชีท6
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#22

Post by snasui »

:D Code ที่ถามมานั้นเป็นการเข้าถึง Sheet6 ไม่เกี่ยวกับ Sheet9 ครับ ช่วยสรุปปัญหานี้มาใหม่ จะได้ลำดับความเข้าใจใหม่ครับ

Code ใดมีปัญหา ตัวอย่างข้อมูลทดสอบคืออะไร ต้องการคำตอบเป็นอย่างไร ถ้าเป็นเรื่องเดียวกันกับกระทู้อื่นต้องไปถามต่อในกระทู้นั้น Procedure เดียวกัน ต้องไม่แยกถามหลายกระทู้ครับ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#23

Post by Benmore »

กระทู้นี้เป็นกระทู้ที่ต้องการให้ค้นหาข้อมูลการเบิกด้วยวันที่ กับ รหัสค่ะ
ตัวอย่าง :ard: :ard: :ard: :ard: :ard: :ard: :ard: :ard: :ard:
ชีทการเบิก(ชีท9)
รหัส 009 |นาย ก |วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 010 |นาย ข |วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 009 |นาย ก |วันที่ 17 srptember 2017 | เบิก เสื้อ 2 ตัว กางเกง 2 ตัว
รหัส 009 |นาย ก |วันที่ 18 srptember 2017 | เบิก เสื้อ 3 ตัว กางเกง 3 ตัว
:ard:
Userform1
เมื่อใส่วันที่ 16 srptember 2017 แล้วกดปุ่มค้นหาก็จะแสดงข้อมูลใน listbox
:arrow: รหัส 009 |นาย ก |เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
:arrow: รหัส 010 |นาย ข |เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
เมื่อใส่รหัส 009 แล้วกดปุ่มค้นหาก็จะแสดงข้อมูลใน listbox
:arrow: วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
:arrow: วันที่ 17 srptember 2017 | เบิก เสื้อ 2 ตัว กางเกง 2 ตัว
:arrow: วันที่ 18 srptember 2017 | เบิก เสื้อ 3 ตัว กางเกง 3 ตัว
แสดงเป็นตารางเพื่อแก้ไขข้อมูลในขั้นตอนถัดไปได้ค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#24

Post by snasui »

Benmore wrote:กระทู้นี้เป็นกระทู้ที่ต้องการให้ค้นหาข้อมูลการเบิกด้วยวันที่ กับ รหัสค่ะ
ตัวอย่าง :ard: :ard: :ard: :ard: :ard: :ard: :ard: :ard: :ard:
ชีทการเบิก(ชีท9)
รหัส 009 |นาย ก |วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 010 |นาย ข |วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
รหัส 009 |นาย ก |วันที่ 17 srptember 2017 | เบิก เสื้อ 2 ตัว กางเกง 2 ตัว
รหัส 009 |นาย ก |วันที่ 18 srptember 2017 | เบิก เสื้อ 3 ตัว กางเกง 3 ตัว
:ard:
Userform1
เมื่อใส่วันที่ 16 srptember 2017 แล้วกดปุ่มค้นหาก็จะแสดงข้อมูลใน listbox
:arrow: รหัส 009 |นาย ก |เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
:arrow: รหัส 010 |นาย ข |เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
เมื่อใส่รหัส 009 แล้วกดปุ่มค้นหาก็จะแสดงข้อมูลใน listbox
:arrow: วันที่ 16 srptember 2017 | เบิก เสื้อ 1 ตัว กางเกง 1 ตัว
:arrow: วันที่ 17 srptember 2017 | เบิก เสื้อ 2 ตัว กางเกง 2 ตัว
:arrow: วันที่ 18 srptember 2017 | เบิก เสื้อ 3 ตัว กางเกง 3 ตัว
แสดงเป็นตารางเพื่อแก้ไขข้อมูลในขั้นตอนถัดไปได้ค่ะ
:D แนบไฟล์ล่าสุดมาใหม่ครับ Code แจ้ง Procedure ปัญหา บรรทัดที่ติดปัญหามาอีกรอบ ที่เคยถามในตอนต้นไม่เกี่ยวกับ Sheet9 แต่เขียนเกี่ยวกับการเข้าถึงข้อมูลที่ Sheet6 ตามด้านล่างครับ
Benmore wrote:

Code: Select all

Private Sub btsearch1_Click()
   Dim found As Boolean
   Dim txt As String
   Dim r As Range
    Dim chkDate As Date
    On Error Resume Next
    Dim nRow As String
    For Each r In Sheet6.Columns(1).SpecialCells(xlCellTypeConstants)
    Sheet6.Activate
    chkDate = CDate(Applicion.Text(cmday & "/" & cmmonth & "/" & cmyear, "ddmmyyyy"))
    If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value = chkDate Then
        nRow = r.row
        found = True
        Exit For
    End If
    Next r
    If found Then
        If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
            MsgBox "กรุณาใส่ข้อมูลเป็นตัวเลข"
            Exit Sub
        End If
        If Err.Number = 91 Then
        TextBox1.RowSource = "txtsearch1.Text"
  TextBox7.Value = ""
    TextBox8.Value = ""
    TextBox9.Value = ""
    TextBox10.Value = ""
      End If
แก้ไขได้ประมาณนี้ค่ะ แต่ก็ยังรันไม่ได้ค่ะ ข้อมูลที่ค้นหาด้วยรหัสก็ไม่แสดงใน listbox เลยค่ะ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#25

Post by Benmore »

ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#26

Post by snasui »

Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
:D เมื่อต้องการบันทึกไปยัง Sheet9 ก็ต้องเขียน Code ให้บันทึกไปยัง Sheet9 ติดปัญหาแล้วเอามาถามกัน ใน Code ข้างต้นนี้เขียนเกียวกับ Sheet6 เกี่ยวกับการ Search สอดคล้องกับหัวกระทู้ที่เขียนว่า "แสดงข้อมูลใน Listbox" ไม่เกียวกับการบันทึก ตามที่ยกมาให้ดูในโพสต์ #24 :roll: ไม่เกี่ยวกับ Sheet9 แต่อย่างใด กรุณาทบทวนดูใหม่ครับ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#27

Post by Benmore »

#24 เป็นการค้นหาข้อมูลค่ะ ไม่ใช่การบันทึกข้อมูล ให้ค้นหาข้อมูลในชีท 9 แล้วมาแสดงบน listbox ค่ะ
snasui wrote:
Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
:D เมื่อต้องการบันทึกไปยัง Sheet9 ก็ต้องเขียน Code ให้บันทึกไปยัง Sheet9 ติดปัญหาแล้วเอามาถามกัน ใน Code ข้างต้นนี้เขียนเกียวกับ Sheet6 เกี่ยวกับการ Search สอดคล้องกับหัวกระทู้ที่เขียนว่า "แสดงข้อมูลใน Listbox" ไม่เกียวกับการบันทึก ตามที่ยกมาให้ดูในโพสต์ #24 :roll: ไม่เกี่ยวกับ Sheet9 แต่อย่างใด กรุณาทบทวนดูใหม่ครับ
#24 เป็นการค้นหาข้อมูลค่ะ ไม่ใช่การบันทึกข้อมูล ให้ค้นหาข้อมูลในชีท 9 แล้วมาแสดงบน listbox ค่ะ ไม่ได้มีคำถามเกี่ยวกับการบันทึกแต่อย่างใด มีแค่การแสดงข้อมูลใน Listbox ค่ะ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#28

Post by Benmore »

Code: Select all

   Dim found As Boolean
   Dim txt As String
   Dim r As Range
    Dim chkDate As Date
    On Error Resume Next
    Dim nRow As String
    chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
    Sheet9.Activate
    For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
    If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then
        nRow = r.Row
        found = True
        Exit For
    End If
    Next r
    If found Then
        If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
            MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
            Exit Sub
        End If
ลองปรับโค้ดให้เชื่อมกับชีท 9 แล้วค่ะ แต่ก็ไม่มีข้อมูลแสดงใน listbox เลยค่ะ แล้วยังค้นหาด้วนวันที่ไม่ได้ค่ะพอกดค้นหาด้วยวันที่จะขึ้นแจ้งเตือนว่าให้ใส่ข้อมูลเป็นตัวเลขเหมือนกับถ้าเราใส่ข้อมูลที่ไม่ใช่ตัวเลขใน txtsearch ค่ะ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#29

Post by snasui »

Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
:D เมื่อปัญหาคือการ Search ก็ไม่ควรกล่าวถึงการบันทึก แยกประเด็นกันให้ชัด การสอบถามควรมุ่งไปที่การอธิบายปัญหาที่เกิดจากการ Search เป็นหลักครับ

สรุปปัญหาคือต้องการค้นหาแล้วให้ข้อมูลแสดงใน ListBox ตัวอย่างการปรับ Code คือด้านล่าง

Code: Select all


'ค้นหา 1
Private Sub btsearch1_Click()
    'On Error Resume Next
    Dim found As Boolean
    Dim txt As String
    Dim r As Range
    Dim chkDate As Date
'    On Error Resume Next
    Dim nRow As String
    chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
    Sheet9.Activate
    For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
        If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then
        nRow = r.Row
        found = True
        Exit For
    End If
    Next r
    If found Then
    '        If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
        '            MsgBox "กรุณาใส่ข้อมูลเป็นตัวเลข"
        '            Exit Sub
        '        End If
        If Err.Number = 91 Then
            TextBox1.RowSource = "txtsearch1.Text"
            TextBox7.Value = ""
            TextBox8.Value = ""
            TextBox9.Value = ""
            TextBox10.Value = ""
            'MsgBox "ไม่มีข้อมูล"
        End If
        TextBox7.Value = Cells(nRow, 1)
        TextBox8.Value = Cells(nRow, 2)
        TextBox9.Value = Cells(nRow, 3)
        TextBox10.Value = Cells(nRow, 4)
        txt = "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)
        ListBox1.AddItem txt
        Exit Sub
    Else
        MsgBox "ไม่มีข้อมูล"
    End If
    Sheet1.Activate
End Sub
โปรดสังเกตว่ามีการ Mark บรรทัดใดไปเป็น Comment บ้าง หากจะใช้จะต้องพิจารณาบรรทัดนั้นใหม่ อีกประการ ListBox ไม่สามารกแยกแสดงเป็นหลายบรรทัดด้วยการใช้ VbCrlf เหมือนเช่น TextBox ที่สามารถกำหนด Multiline ช่วยเพื่อแสดงข้อความที่คั่นด้วย VbCrlfเป็นหลายบรรทัดครับ
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#30

Post by Benmore »

Benmore wrote:
snasui wrote:
Benmore wrote:ข้อมูลทุกอย่าง ถูกบันทึกลงในชีท9ค่ะ
:D เมื่อปัญหาคือการ Search ก็ไม่ควรกล่าวถึงการบันทึก แยกประเด็นกันให้ชัด การสอบถามควรมุ่งไปที่การอธิบายปัญหาที่เกิดจากการ Search เป็นหลักครับ

สรุปปัญหาคือต้องการค้นหาแล้วให้ข้อมูลแสดงใน ListBox ตัวอย่างการปรับ Code คือด้านล่าง

Code: Select all

แล้วต้องแก้ไขโค้ดตรงไหนเพิ่มค่ะ


'ค้นหา 1
Private Sub btsearch1_Click()
    'On Error Resume Next
    Dim found As Boolean 
    Dim txt As String
    Dim r As Range
    Dim chkDate As Date
'    On Error Resume Next
    Dim nRow As String
    chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
    Sheet9.Activate
    For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
        If Right(r.Value, 3) = Right(txtsearch1.Text, 3) Or r.Offset(0, 4).Value2 = CLng(chkDate) Then
        nRow = r.Row
        found = True
        Exit For
    End If
    Next r
    If found Then
    '        If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
        '            MsgBox "กรุณาใส่ข้อมูลเป็นตัวเลข"
        '            Exit Sub
        '        End If
        If Err.Number = 91 Then
            TextBox1.RowSource = "txtsearch1.Text"
            TextBox7.Value = ""
            TextBox8.Value = ""
            TextBox9.Value = ""
            TextBox10.Value = ""
            'MsgBox "ไม่มีข้อมูล"
        End If
        TextBox7.Value = Cells(nRow, 1)
        TextBox8.Value = Cells(nRow, 2)
        TextBox9.Value = Cells(nRow, 3)
        TextBox10.Value = Cells(nRow, 4)
        txt = "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)
        ListBox1.AddItem txt
        Exit Sub
    Else
        MsgBox "ไม่มีข้อมูล"
    End If
    Sheet1.Activate
End Sub
โปรดสังเกตว่ามีการ Mark บรรทัดใดไปเป็น Comment บ้าง หากจะใช้จะต้องพิจารณาบรรทัดนั้นใหม่ อีกประการ ListBox ไม่สามารกแยกแสดงเป็นหลายบรรทัดด้วยการใช้ VbCrlf เหมือนเช่น TextBox ที่สามารถกำหนด Multiline ช่วยเพื่อแสดงข้อความที่คั่นด้วย VbCrlfเป็นหลายบรรทัดครับ
ต้องแก้ไขโค้ดตรงไหนค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#31

Post by snasui »

Benmore wrote:ต้องแก้ไขโค้ดตรงไหนค่ะ
:D คำถามคืออะไรครับ กรุณาเขียนมาด้วยจะได้ทราบว่าพูดถึงประเด็นใด ถ้าเป็นเรื่องการแสดง ListBox เป็นหลายบรรทัดเหมือน TextBox จะทำเช่นนั้นไม่ได้ ไม่มี Propety นั้นที่เป็นของ ListBox ถ้าจะทำต้องกลับไปใช้ TextBox

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

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

#32

Post by Benmore »

ถ้าเลือกใช้แบบ textbox จะสามารถเลือกข้อมูลมาแก้ไข ลบ ได้หรือไม่ค่ะ
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#33

Post by snasui »

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

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

#34

Post by Benmore »

รบกวนช่วยดูโค้ดการค้นหาข้อมูลด้วยรหัสพร้อมกับวันที่ให้หน่อยค่ะ ตอนนี้ค้นหาได้โดยรหัสอย่างเดียวค่ะ
เช่น ค้นหา รหัส 02000009 วันที่ 20 November 2017
แสดง :arrow: การเบิกของของรหัส 02000002 ในวันที่ 20 November 2017 ถ้าไม่มีข้อมูลให้แสดงว่าไม่มีข้อมูล

Code: Select all

Private Sub btsearch1_Click()
On Error Resume Next
If txtsearch1.Value = "" Or cmday.Value = "" Or cmmonth.Value = "" Or cmyear.Value = "" Then
                MsgBox "¡ÃسÒãÊè¢éÍÁÙÅãËé¤Ãº"
                Combobox1.Value = ""
        Exit Sub
    End If
   Dim found As Boolean
   Dim txt As String
   Dim r As Range
    Dim chkDate As Date
    Dim nRow As String
    chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
    Sheet9.Activate
    For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
    If Right(r.Value, 3) = Right(txtsearch1.Value, 3) Or r.Offset(0, 0).Value2 = CLng(chkDate) Then
        nRow = r.Row
        found = True
        Exit For
    End If
    Next r
    If found Then
        If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
            MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
            Exit Sub
        End If
        If Err.Number = 91 Then
        'TextBox1.RowSource = "txtsearch1.Text"
  TextBox7.Value = ""
    TextBox8.Value = ""
    TextBox9.Value = ""
    TextBox10.Value = ""
      TextBox11.Value = ""
         TextBox16.Value = ""
            TextBox17.Value = ""
        TextBox12.Value = ""
          TextBox13.Value = ""
    'MsgBox "äÁèÁÕ¢éÍÁÙÅ"
    End If
 TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 2)
    TextBox9.Value = Cells(nRow, 3)
    TextBox10.Value = Cells(nRow, 4)
      TextBox11.Value = Cells(nRow, 6)
      TextBox16.Value = Cells(nRow, 7)
      TextBox17.Value = Cells(nRow, 8)
        TextBox12.Value = Cells(nRow, 9)
          TextBox13.Value = Cells(nRow, 10)
     Exit Sub
     Else
       MsgBox "äÁèÁÕ¢éÍÁÙÅ"
    End If
    Sheet1.Activate
End Sub
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#35

Post by snasui »

:D ตัวอย่างการค้นหา 2 เงื่อนไขพร้อมกันครับ

Code: Select all

'Other code
If Right(r.Value, 3) = Right(txtsearch1.Value, 3) And r.Offset(0, 4).Value2 = CLng(chkDate) Then
    nRow = r.Row
    found = True
    Exit For
End If
'Other code
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#36

Post by Benmore »

snasui wrote::D ตัวอย่างการค้นหา 2 เงื่อนไขพร้อมกันครับ

Code: Select all

'Other code
If Right(r.Value, 3) = Right(txtsearch1.Value, 3) And r.Offset(0, 4).Value2 = CLng(chkDate) Then
    nRow = r.Row
    found = True
    Exit For
End If
'Other code
ตรงนี้ทำได้แล้วขอบคุณมากค่ะ รบกวนดูตรง OptionButton ให้หน่อยค่ะ
เช่น ถ้าต้องการค้นหาข้อมูลแล้วมีสถานะว่ามารับแล้ว ให้ OptionButton "มารับแล้ว" ถูกติ๊กค่ะ

Code: Select all

Private Sub btsearch1_Click()
On Error Resume Next
If txtsearch1.Value = "" Or cmday.Value = "" Or cmmonth.Value = "" Or cmyear.Value = "" Then
                MsgBox "¡ÃسÒãÊè¢éÍÁÙÅãËé¤Ãº"
                Combobox1.Value = ""
        Exit Sub
    End If
   Dim found As Boolean
   Dim txt As String
   Dim r As Range
    Dim chkDate As Date
    Dim nRow As String
    chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
    Sheet9.Activate
    For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
    If Right(r.Value, 3) = Right(txtsearch1.Value, 3) Or r.Offset(0, 0).Value2 = CLng(chkDate) Then
        nRow = r.Row
        found = True
        Exit For
    End If
    Next r
    If found Then
        If Not IsNumeric(VBA.Right(txtsearch1.Text, 3)) Then
            MsgBox "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
            Exit Sub
  TextBox7.Value = ""
    TextBox8.Value = ""
    TextBox9.Value = ""
    TextBox10.Value = ""
      TextBox11.Value = ""
         TextBox16.Value = ""
            TextBox17.Value = ""
        TextBox12.Value = ""
          If OptionButton9.Value = True Then
    ElseIf OptionButton10.Value = True Then
        ElseIf OptionButton11.Value = True Then
        OptionButton11.Value = TextBox13.Value
    End If
    'MsgBox "äÁèÁÕ¢éÍÁÙÅ"
    End If
 TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 2)
    TextBox9.Value = Cells(nRow, 3)
    TextBox10.Value = Cells(nRow, 4)
      TextBox11.Value = Cells(nRow, 6)
      TextBox16.Value = Cells(nRow, 7)
      TextBox17.Value = Cells(nRow, 8)
        TextBox12.Value = Cells(nRow, 9)
           If OptionButton9.Value = True Then
           OptionButton9.Value = Cells(nRow, 10)
    ElseIf OptionButton10.Value = True Then
    OptionButton10.Value = Cells(nRow, 10)
        ElseIf OptionButton11.Value = True Then
        TextBox13.Value = Cells(nRow, 10)
    End If
     Exit Sub
     Else
       MsgBox "äÁèÁÕ¢éÍÁÙÅ"
    End If
    Sheet1.Activate
End Sub
You do not have the required permissions to view the files attached to this post.
Benmore
Bronze
Bronze
Posts: 254
Joined: Tue Sep 05, 2017 9:51 am

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

#37

Post by Benmore »

:?: :?: :?: :shock: :shock: :shock:
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#38

Post by snasui »

Benmore wrote: :?: :?: :?: :shock: :shock: :shock:
:D ฟอรัมนี้ไม่อนุญาตให้มีการดันกระทู้ด้วยการโพสต์โดยไม่มีคำถามหรือข้อความอื่นใดครับ

หากตอบไม่ทันใจหรือช้าไปบ้างเป็นเพราะผู้ตอบอาจจะติดภารกิจ

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

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

#39

Post by Benmore »

ตรงนี้ทำได้แล้ว ขอบคุณค่ะ
ขอสอบถามเพิ่มเติมค่ะ
:ard:
:arrow: Userform10 :arl:
ถ้าต้องการค้นหาข้อมูลด้วยรหัสพนักงานกับเดือน ต้องเพิ่มโค้ดตรงไหนค่ะ
เช่น ถ้าต้องการค้าหาข้อมูลการเบิกชุดของพนักงานรหัส 009 ในเดือน November ให้แสดงข้อมูลการเบิกชุดทั้งหมดในชีทการเบิกของพนักงานรหัส 009 ในเดือน November ค่ะ

Code: Select all

Private Sub CommandButton5_Click()
 If txtsearch.Value = "" Or Commonth.Value = "" Then
                MsgBox "¡ÃسÒàÅ×Í¡¢éÍÁÙÅ¡è͹"
        Exit Sub
        End If
 Dim found As Boolean
   Dim txt As String
   Dim r As Range
    On Error Resume Next
    Dim nRow As String
    For Each r In Sheet9.Columns(1).SpecialCells(xlCellTypeConstants)
    Sheet9.Activate
        If Right(r.Value, 3) = Right(txtsearch.Text, 3) 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 "¡ÃسÒãÊè¢éÍÁÙÅà»ç¹µÑÇàÅ¢"
            Exit Sub
        End If
        If Err.Number = 91 Then
        TextBox1.RowSource = "txtsearch.Text & combobox1.value"
    End If
   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 "äÁèÁÕ¢éÍÁÙÅ"
    End If
End Sub
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30736
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

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

#40

Post by snasui »

: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
Post Reply