循环所有表单不工作,只能在单张表单上执行

我希望下面的代码能够在工作簿上的所有(20)表单上工作,但是它只能在1个表单上工作,并在该表单上反复复制/粘贴,有什么build议吗?

非常感谢您的时间

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 

你必须指定你正在使用的对象(ws)

 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 

你也可以用With...End With来更优雅