VBA运行时错误1004应用程序定义或对象定义的错误

时间:2018-05-01 13:55:02

标签: excel vba excel-vba

您好我是VB的新手,我正在尝试将数据从一张工作簿复制到另一张工作簿,但是当我尝试这样做时,我收到的错误是:

  

“错误1004:应用程序定义或对象定义的错误”

相同的代码已经提供了所需的输出,但每次都不起作用。

Working文件Weather Dashboard中的Master文件Sheet1工作表中搜索用户选择日期的数据,然后粘贴到Weather Dashboard

主文件包含200000行数据,要复制的总数据大约为1000行。

Private Sub CommandButton1_Click()
    Windows("Weather Dashboard.xlsm").Activate
    stra = Worksheets("Working").Cells(1, 1)

    'stores user input date as integer in stra
    Workbooks.Open Filename:="C:\Users\Desktop\Master.xlsx"
    Worksheets("Sheet1").Select
    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

    For j = 0 To 9
        For i = 2 To a
            'Search for the chosen date & following 9 days from Master File
            If Worksheets("Sheet1").Cells(i, 5).Value = stra + j Then
                Worksheets("Sheet1").Rows(i).Copy
                Windows("Weather Dashboard.xlsm").Activate
                b = Worksheets("Working").Cells(Rows.Count, 1).End(xlUp).Row
                Worksheets("Working").Cells(b + 1, 1).Select

                ActiveSheet.Paste

                Windows("Master.xlsx").Activate
                Worksheets("Sheet1").Activate
            End If
        Next i
    Next j
    Application.CutCopyMode = False
End Sub

1 个答案:

答案 0 :(得分:0)

我只是在这里重写以删除隐式引用:不解决任何其他问题

Option Explicit
Private Sub CommandButton1_Click()
dim a as long
dim b as long
Dim Stra as integer
dim wbDash as workbook
set wbDash =  workbooks("Weather Dashboard.xlsm")   
stra =wbdash.Worksheets("Working").Cells(1, 1)
dim wb as workbook
'stores user input date as integer in stra
set wb = Workbooks.Open("C:\Users\Desktop\Master.xlsx")
with wb.Worksheets("Sheet1")
    a = .Cells(.Rows.Count, 1).End(xlUp).Row

    For j = 0 To 9
        For i = 2 To a
            'Search for the chosen date & following 9 days from Master File
            If .Cells(i, 5).Value = stra + j Then
                 b = wbdash.Worksheets("Working").Cells(Rows.Count, 1).End(xlUp).Row
               .Rows(i).Copy _
                  destination:=
                wbdash.Worksheets("Working").Cells(b + 1, 1)


            End If
        Next i
    Next j
end with   
End Sub

我认为这会消除你的错误