2017-08-22 95 views
1

我希望下面的代碼可以在工作簿上的所有(20)工作表上工作,但它只能在1張工作表上工作,並且在該工作表上反覆複製/粘貼,有什麼建議嗎?loop all sheet-not working,only re performed on single sheets

非常感謝您的時間

Sub Copylastcolumn() 

Dim ws As Worksheet 
On Error Resume Next 
For Each ws In ThisWorkbook.Worksheets 

Dim LastRow As Long 
Dim LastCol As Long 

LastRow = Cells(Rows.Count, "A").End(xlUp).Row 
LastCol = Cells(6, "BJ").End(xlToLeft).Column 

Cells(6, LastCol).Resize(LastRow - 5).Copy Cells(6, LastCol + 1) 

Columns(LastCol).Copy 
Columns(LastCol).PasteSpecial xlPasteValues 

Next ws 

End Sub 

回答

1

你必須指定要與

Sub Copylastcolumn() 

Dim ws As Worksheet 
Dim LastRow As Long 
Dim LastCol As Long 

On Error Resume Next 
For Each ws In ThisWorkbook.Worksheets 
    LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row 
    LastCol = ws.Cells(6, "BJ").End(xlToLeft).Column 

    ws.Cells(6, LastCol).Resize(LastRow - 5).Copy ws.Cells(6, LastCol + 1) 

    ws.Columns(LastCol).Copy 
    ws.Columns(LastCol).PasteSpecial xlPasteValues 

Next ws 

End Sub 

工作對象(WS)您也可以使用With...End With更優雅