Hi-Im使用此公式循环浏览特定的工作表,然后获得某个范围并将其复制到另一张工作表。当我第一次运行宏时,它在第一个循环中是成功的。但一旦它返回并重新开始循环,就会出现错误。。
错误说。
运行时错误1004:对象"_Worksheet"的方法"Range"失败。
这是高亮显示的行:
ws.Range(StartCell, ws.Cells(LastRow, LastColumn)).Select
任何帮助都将不胜感激。
Sub sheetLoop()
Application.Calculation = xlCalculationManual
Dim ws As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set StartCell = Range("A10")
Windows("PMP Open Seats Report - 11-05-20 -.xls").Activate
For Each ws In Worksheets
If ws.Name = "16 - 30 Days" Or ws.Name = "31 - 60 Days" Or ws.Name = "61 - 90 Days" Or ws.Name = "90+ Days" Then
With ws
ws.Activate
LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column
MsgBox LastRow
MsgBox LastColumn
MsgBox ws.Name
'Select Range
ws.Range(StartCell, ws.Cells(LastRow, LastColumn)).Copy
Selection.Copy
Sheets("RAW").Activate
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
End With
End If
Next ws
Application.Calculation = xlCalculationAutomatic
End Sub
StartCell
不是完全限定的,因此它是基于您给它一个值时处于活动状态的任何工作表。
Set StartCell = Range("A10")
在ws
具有值之后,在循环中移动该行,然后包括图纸参照。
With ws
ws.Activate
Set StartCell = ws.Range("A10")
看看这是否符合你的期望。