从一个工作表复制到一个工作表时出现错误1004

时间:2015-07-25 21:09:11

标签: excel-vba vba excel

我在下面的代码中收到错误1004。目标是将大约30张具有A1中相同单元格值的纸张组合成一张名为“数据”的纸张。

Sub CombineSheets()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    --create data sheet to hold all the data
    Sheets.Add.Name = "Data"
    --define various variables
    Dim WS_Count As Integer
    Dim I As Integer
    Dim lastrow As Long
    --insert header rows
    Sheets("Data").Range("A1") = "Phone"
    Sheets("Data").Range("B1") = "Area"
    Sheets("Data").Range("C1") = "Property"
    Sheets("Data").Range("D1") = "Value"
    --count the number of sheets
    WS_Count = ActiveWorkbook.Worksheets.Count
    --for each sheet with the cell A1 = "Cleaned" add to Data sheet
    For I = 1 To WS_Count
        lastrow = ActiveWorkbook.Sheets("Data").Cells(1048576, 3).End(xlUp).Row
        ActiveWorkbook.Sheets(I).Activate
        If ActiveSheet.Range("A1") = "Cleaned" Then
        --perform copy
            ActiveSheet.UsedRange.Copy _
               Destination:=Worksheets("Data").Range(Cells(lastrow + 1, 1))
    End If
Next I
Application.ScreenUpdating = True
End Sub

我做错了什么?我认为它是在复制步骤中,但我需要使用ActiveSheet在所有工作表上执行该功能。任何建议

1 个答案:

答案 0 :(得分:0)

ActiveSheet.UsedRange.Copy _
     Destination:=Worksheets("Data").Range(Cells(lastrow + 1, 1))

应该是:

ActiveSheet.UsedRange.Copy _
     Destination:=Worksheets("Data").Cells(lastrow + 1, 1)