snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
Sub Run_03()
Dim n As Long
Dim i As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
On Error Resume Next
For n = 5 To 16
If Month(ws2.Cells(2, n)) = Month(Date) Then
For i = 6 To ws1.Range("a" & ws1.Rows.Count).End(xlUp).Row
ws1.Cells(i, 5).Value = Application.WorksheetFunction.Index(ws2.Range("c3:p300"), _
Application.WorksheetFunction.Match(ws1.Cells(i, 1), ws2.Range("c3:c300"), 0), _
Application.WorksheetFunction.Match(ws1.Cells(4, 5), ws2.Range("e2:p2"), 0))
Next i
End If
Next n
End Sub
You do not have the required permissions to view the files attached to this post.
March201711 wrote: ↑Thu Mar 22, 2018 9:06 am
สอบถามหน่อยค่ะ ถ้าจะเปลี่ยนเป็นเดือนอื่น เช่น เป้น เดือน jan หรือ feb จะได้ไหมคะ พอเปลี่ยน column O2 แล้ว กด refresh ข้อมูลก็ run เป็นเดือน mar ค่ะ อยากให้ run เป็นเดือนต่อๆไป โดยดูจาก cell O2 เป็นหลักค่ะ
Sub Run_03()
Dim n As Long
Dim i As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
On Error Resume Next
With Application.WorksheetFunction
For n = 5 To 16
If Month(ws2.Cells(2, n)) = Month(ws1.Cells(2, 15)) Then
For i = 6 To ws1.Range("a" & ws1.Rows.Count).End(xlUp).Row
ws1.Cells(i, n - 2).Value = .Index(ws2.Range("e3:p300"), _
.Match(ws1.Cells(i, 1), ws2.Range("c3:c300"), 0), _
.Match(ws1.Cells(4, Month(ws1.Cells(2, 15)) + 2), ws2.Range("e2:p2"), 0))
Next i
End If
Next n
End With
End Sub