0

我有一个宏在第一个工作表上运行良好,但在尝试转到下一个工作表时崩溃。我收到运行时错误 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
4

1 回答 1

0

当您没有正确引用工作表名称时,会发生您描述的错误,在这种情况下,我认为它是一个可能包含空格的工作表名称。尝试用单引号或双引号将工作表名称括起来:

... "='" & Worksheets(i).Name & "'!" ...

于 2013-07-22T17:21:14.117 回答