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

时间:2013-06-28 05:19:51

标签: excel excel-vba excel-2010 vba

大家好,我运行以下代码时得到错误1004。基本上,代码将我的工作表与其他工作簿中的工作表进行比较并复制数据。我知道这个错误很简单,我可以弄清楚,但我在截止日期..请告知

Sub test()

    Dim lastCol, lastRow As Long, k, e As Long, a As Variant, b As Variant, cmpRng As Range
    Dim mastCol As Long, mastRng As Range, n As Long
    Dim Wbk As Workbook
    Dim file As String
    Dim SelectedFiles As Object
    Dim filename As Variant
    Dim indx As Long
    Dim t As Integer


    ChDrive "G:\"                   ' To set the drive where the files are located.
    ChDir "g:\work"                 'To set the folder where the files are located. This is done to save time locating the folder through pop up box always.    


       Application.ScreenUpdating = False   
       Sheets("Temp Calc").Select  
           'Clear existing sheet data ecept header. 
       Rows(1).Offset(1, 0).Resize(Rows.Count - 1).ClearContents

     filename = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xlsx), *.xlsx", Title:="Get File", MultiSelect:=True) 

      Worksheets("Temp Calc").Select

    lastCol = Worksheets("Temp Calc").Cells(1, Columns.Count).End(xlToLeft).Column
    lastRow = Worksheets("Temp Calc").Cells(Rows.Count, 1).End(xlDown).Row

    Set cmpRng = Range(Cells(1, 1), Cells(1, lastCol))
    a = cmpRng
    mastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Set mastRng = Range(Cells(1, 1), Cells(1, mastCol))
    b = mastRng

    For t = 1 To UBound(filename)
            Workbooks.Open (filename(t))
            For k = 1 To lastCol
            For n = 1 To mastCol
                If UCase(a(1, k)) = UCase(b(1, n)) Then           
                Worksheets("Sheet1").Range(Cells(2, n), Cells(lastRow, n)).Copy       
                Windows("Dashboard_for_Roshan.xlsm").Activate
                Worksheets("Temp Calc").Select
                Cells(2,k).PasteSpecial Paste:=xlPasteAll,Operation:=xlNone,SkipBlanks:=False,Transpose:=False           
    ****<  I get an error for the above line  >****

        Exit For        
        End If
        Next
    Next

    Next t    
    Application.ScreenUpdating = True      

End Sub

1 个答案:

答案 0 :(得分:0)

替换

Worksheets("Temp Calc").Select
            Cells(2,k).PasteSpecial Paste:=xlPasteAll,Operation:=xlNone,SkipBlanks:=False,Transpose:=False    

Application.Goto Worksheets("Temp Calc").Cells(2, k)
ActiveSheet.Paste