2013-06-28 87 views
1

你好,我所有我得到錯誤1004當我運行下面的代碼。基本上,代碼將我的工作表與其他工作簿中的工作表進行比較並複製數據。我知道這個錯誤很簡單,我自己看着辦吧,但我在最後期限..請指教運行時錯誤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

感謝編輯brettdj,我仍然必須掌握正確的格式化概念:) – mathew

+0

hello mehow, 這很尷尬,因爲我的問題的措辭完全與上面的第一個地方一樣... – mathew

回答

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 
+0

yesssssssssssssssss s的錯誤消失了隊友,但它不粘貼 – mathew

+0

嗯 - 它在我的測試中工作得很好。你複製的範圍是空白的嗎? – brettdj

+0

沒有隊友裏面有數據... – mathew

相關問題