
SQL กับ VBA มันคนละ Concept ครับ
ลักษณะความแตกต่างเปรียบเทียบเพื่อให้เห็นภาพชัด
SQL : ที่ตู้ A ต้องการกาแฟซองสีเขียว จะได้กาแฟพร้อมดื่ม
VBA : ไปที่ตู้ A, เปิดตู้ A, หยิบกาแฟซองสีเขียว, ฉีกซองกาแฟ, หยิบถ้วยกาแฟ, เทกาแฟใส่ถ้วย, นำถ้วยไปรองน้ำร้อน, กดน้ำร้อน, หยิบช้อน, คนกาแฟในถ้วย, ยกถ้วยกาแฟ, ดื่มกาแฟ
ผมปรับปรุงไฟล์มาให้ใหม่ ในไฟล์ Form เพิ่ม Sheet Temp เพื่อรองรับข้อมูล และใน Userform เปลี่ยน Label เป็น ListBox ลองดูไฟล์แนบประกอบครับ
Code: Select all
Private Sub btnShow_Click()
Dim rt As Range, rs As Range, rsall As Range
Dim s As String, RowSrc As String, rtp As Range
s = Application.Text(Date, "dดดดดbb")
Worksheets("Temp").Range("A:I").ClearContents
With Workbooks("รายงานประจำเดือน.xlsx").Worksheets(s)
Set rsall = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
End With
For Each rs In rsall
If ob1.Value = True Then
If rs = CLng(Me.TextBox1) And CLng(rs.Offset(0, -2)) = CLng(Date) _
And rs.Offset(0, 6) = Me.ob1.Caption Then
Set rt = ThisWorkbook.Worksheets("Temp") _
.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
rs.Offset(0, -2).Resize(1, 9).Copy
rt.PasteSpecial xlPasteValues
End If
ElseIf ob2.Value = True Then
If rs = CLng(Me.TextBox1) And CLng(rs.Offset(0, -2)) = CLng(Date) _
And rs.Offset(0, 6) = Me.ob2.Caption Then
Set rt = ThisWorkbook.Worksheets("Temp") _
.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
rs.Offset(0, -2).Resize(1, 9).Copy
rt.PasteSpecial xlPasteValues
End If
End If
Next rs
With Worksheets("Temp")
Set rtp = .Range("A2", .Range("I" & Rows.Count).End(xlUp))
End With
RowSrc = "Temp!" & rtp.Address
Me.ListBox1.RowSource = RowSrc
End Sub
Private Sub UserForm_Initialize()
Me.lblDate = Date
Me.ListBox1.RowSource = ""
With Application
Me.Width = .Width
Me.Height = .Height
Me.Top = .Top
Me.Left = .Left
End With
End Sub
You do not have the required permissions to view the files attached to this post.