EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
'ค้นหา
Private Sub btsearch1_Click()
Dim found As Boolean
Dim txt As String
Dim r As Range
On Error Resume Next
Dim nRow As String
For Each r In Sheet6.Columns(1).SpecialCells(xlCellTypeConstants)
Sheet6.Activate
If Right(r.Value, 3) = Right(txtsearch1.Text, 3) 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
ListBox1.RowSource = "txtsearch1.Text"
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
MsgBox "ไม่มีข้อมูล"
End If
TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 8)
TextBox9.Value = Cells(nRow, 2)
TextBox10.Value = Cells(nRow, 13)
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.Value = txt
Exit Sub
End If
Sheet1.Activate
End Sub
คีย์วันที่ให้ค้นหาวันที่ หากคีย์วันที่ให้ค้นหาวันที่ คีย์รหัสพนักงานให้ค้นหาตามรหัสพนักงาน ค่ะsnasui wrote: การค้นหาข้อมูลด้วยวันที่ เป็นการค้นหาร่วมกับรหัสพนักงานหรือว่าเลือกอย่างใดอย่างหนึ่ง เช่น หากคีย์วันที่ให้ค้นหาวันที่ หากคีย์รหัสพนักงานให้ค้นหาตามรหัสพนักงาน ฯลฯ
กรณีค้นหาตามรหัสพนักงานแล้วข้อมูลไม่ขึ้น ช่วยยกตัวอย่างรหัสพนักงานมาด้วยจะได้สะดวกต่อการทดสอบครับ
btsearch1_Click
ให้สร้างตัวแปรวันที่สำหรับเปรียบเทียบกับค่าในคอลัมน์ E ของชีต EGAS_Data สมมุตชื่อว่า chkDateCode: Select all
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
Code: Select all
ค้นหา1
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
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 = ""
'MsgBox "ไม่มีข้อมูล"
End If
TextBox7.Value = Cells(nRow, 1)
TextBox8.Value = Cells(nRow, 8)
TextBox9.Value = Cells(nRow, 2)
TextBox10.Value = Cells(nRow, 13)
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.Value = txt
Exit Sub
Else
MsgBox "ไม่มีข้อมูล"
End If
Sheet1.Activate
End Sub
chkDate
กำหนดไว้ที่บรรทัดไหนครับchkDate
เสียก่อนแล้วค่อยนำไปใช้ครับCode: Select all
Dim chkDate As Date
On Error Resume Next
Dim nRow As String
For Each r In Sheet6.Columns(1).SpecialCells(xlCellTypeConstants)
Sheet6.Activate
chkDate = cmday Or cmmonth Or cmyear
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
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
ค้นหาข้อมูลด้วยวันเดือนปีไม่ได้ค่ะsnasui wrote: กรุณาแจังปัญหาพร้อมไฟล์แนบที่มี Code ล่าสุดเรียบร้อยแล้วพร้อมกับข้อมูลที่จะทดสอบเสมอ จะได้สะดวกกับผู้ตอบที่จะทดสอบได้ทันทีครับ
ตัวอย่างค่ะ ถ้าเลือกวันที่ 16 November 2017 แล้วกดปุ่มค้นหาให้ข้อมูลในชีทการเบิกที่มีข้อมูลตรงกับวันที่ ที่เราต้องการ แสดงข้อมูลทั้งหมดที่มีวันที่ตรงกันใน listbox ค่ะsnasui wrote: วันที่เท่าไร เดือนใด ปีใดครับ รหัสใดครับ แจ้งมาให้ชัด ๆ ครับ
ถ้าเอาเงื่อนไขการตรวจสอบวันที่โปรแกรมทำงานถูกต้องหรือไม่ แจ้งมาด้วยครับ
Code: Select all
Dim chkDate As Date
On Error Resume Next
Dim nRow As String
chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday)
Sheet6.Activate
For Each r In Sheet6.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
ปรับตามแล้วก็ยังไม่แสดงอะไรแม้กระทั่งค้นหาด้วยรหัสค่ะsnasui wrote: ตัวอย่างการปรับ Code ครับ
กรุณาทดสอบด้วยวันที่ทีมีอยู่จริงในชีต EGAS_Data ไม่ใช่วันที่ 16/11/2017 ยกเว้นจะมีวันที่นี้อยู่ด้วยครับCode: Select all
Dim chkDate As Date On Error Resume Next Dim nRow As String chkDate = DateSerial(cmyear, cmmonth.ListIndex + 1, cmday) Sheet6.Activate For Each r In Sheet6.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