我有一个宏在第一个工作表上运行良好,但在尝试转到下一个工作表时崩溃。我收到运行时错误 1004 应用程序定义或对象定义错误。
我的代码之前返回了单元格的值,并且循环正常。我更改了代码,因为我的源数据中有空白单元格,当它复制并粘贴到我的主工作表时,它留下了一个空白单元格来抵消我的数据。
在我更改宏以提取单元地址而不是值后,它停止循环。
我的代码是
Sub Test()
Dim imaxrow As Double
Dim i As Double
Dim wscount As Double
wscount = ActiveWorkbook.Worksheets.Count
For i = 2 To wscount
'Rows of data to be extracted
imaxrow = 22 'Max rows
'Use to offset columns to next dataset
For Each e In Array(0, 11)
'Starting row to copy data
For irow = 10 To imaxrow
If Worksheets(i).Cells(irow, 21 + e).Value = "" Then
'Nothing in this cell.
'Do nothing.
Else
' Copy Cell data to mastersheet
Worksheets("macro test sheet").Cells(Rows.Count, "F").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(irow, 21 + e).Address
Worksheets("macro test sheet").Cells(Rows.Count, "G").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(irow, 22 + e).Address
Worksheets("macro test sheet").Cells(Rows.Count, "H").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(irow, 23 + e).Address
Worksheets("macro test sheet").Cells(Rows.Count, "I").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(irow, 24 + e).Address
Worksheets("macro test sheet").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(irow, 4).Address
Worksheets("macro test sheet").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(irow, 3).Address
Worksheets("macro test sheet").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(5, 1).Address
Worksheets("macro test sheet").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(6, 21 + e).Address
Worksheets("macro test sheet").Cells(Rows.Count, "E").End(xlUp).Offset(1, 0) = "=" & Worksheets(i).Name & "!" & Worksheets(i).Cells(7, 21 + e).Address
End If
Next irow
Next e
Next i
End Sub