我想复制粘贴工作簿中所有工作表的最后一行工作表的第一个单元格,而我的代码不能正常工作,代码只在活动工作表上完成。
Sub Macro5()
'
' Macro5
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Range("A2").Copy Destination:=Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Next ws
End Sub
感谢您的帮助
答案 0 :(得分:3)
循环工作表不会使它们处于活动状态。你想要这样的东西。
Dim WS As Excel.Worksheet
Dim iIndex As Integer
For iIndex = 1 To ActiveWorkbook.Worksheets.count
Set WS = Worksheets(iIndex)
With WS
'Do something here.
.Range("A2").Copy Destination:=.Range("A3:A" & .Cells(.Rows.Count, "B").End(xlUp).Row)
End With
Next iIndex
你可以在循环中使用ws,但我总是设置对象。
For Each ws In ActiveWorkbook.Worksheets
ws.Range("A2").Copy Destination:=ws.Range("A3:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Next ws