snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
For i = 11 To 2000
Set X = Worksheets("account").[C2]
Set O = Worksheets("account").[C4]
If Cells(i, "D").Value = O And Cells(i, "A").Value = X And Cells(i, "J") <> "" Or Cells(i, "J") = X Then
Range(Cells(i - 1, 1), Cells(i - 1, 4)).Copy
Range(Cells(i, 1), Cells(i, 4)).Select
ActiveSheet.Paste
ElseIf Cells(i, "D").Value <> O And Cells(i, "A").Value = X And Cells(i, "J") <> "" Or Cells(i, "J") = X Then
Range(Cells(i - 1, 1), Cells(i - 1, 3)).Copy
Range(Cells(i, 1), Cells(i, 3)).Select
ActiveSheet.Paste
Sub Test()
Dim rAll As Range
Dim r As Range
With Worksheets("Payable")
Set rAll = .Range("D11", .Range("J" & Rows.Count) _
.End(xlUp).Offset(0, -6))
End With
For Each r In rAll
With Application
r = .Trim(r)
If Len(r) = 0 And Len(.Trim(r.Offset(0, -3))) = 0 Then
r.Offset(0, -3).Resize(1, 4).ClearContents
ElseIf Len(r) <> 0 And Len(.Trim(r.Offset(0, -1))) = 0 Then
r.Offset(0, -3).Resize(1, 3).ClearContents
End If
End With
Next r
End Sub
หนูได้นำไปใช้แล้วปรากฏรันในเครื่องหนูทำได้แต่พอไปรันในเครื่องคนอื่นมันรัน Sub ที่อาจารย์ให้มานานมากจนเหมือนมันค้าง ทั้งๆที่หนูเปลี่ยนที่อยู่ไฟล์เรียบร้อยแล้วค่ะ
หนูจึงขอรบกวนถามว่าเพื่อความเข้าใจว่า
With Worksheets("Payable")
Set rAll = .Range("D11", .Range("J" & Rows.Count) _
.End(xlUp).Offset(0, -6))
End With
With Application
r = .Trim(r)
If Len(r) = 0 And Len(.Trim(r.Offset(0, -3))) = 0 Then
r.Offset(0, -3).Resize(1, 4).ClearContents
ElseIf Len(r) <> 0 And Len(.Trim(r.Offset(0, -1))) = 0 Then
r.Offset(0, -3).Resize(1, 3).ClearContents
End If
End With
คือการใช้ Trim ตัดข้อมูลที่เป็นการเคาะออกใช่ใหม่ค่ะ แล้วทำไมต้องใช้ Resize ด้วยค่ะ
Gin R wrote:With Application
r = .Trim(r)
If Len(r) = 0 And Len(.Trim(r.Offset(0, -3))) = 0 Then
r.Offset(0, -3).Resize(1, 4).ClearContents
ElseIf Len(r) <> 0 And Len(.Trim(r.Offset(0, -1))) = 0 Then
r.Offset(0, -3).Resize(1, 3).ClearContents
End If
End With
คือการใช้ Trim ตัดข้อมูลที่เป็นการเคาะออกใช่ใหม่คะ