2015-06-20 28 views
0

這是建立在Excel 2007中的宏,需要運行的時間過長,最後顯示錯誤消息VBA是不是在Excel工作365個親加 - 「範圍類的PasteSpecial方法失敗」

「運行時間錯誤1004 - 範圍類的pastespecial方法失敗「。

雖然在Excel 2007中相同的宏工作得很好,但在30秒內完成沒有任何錯誤。

請檢查並建議。

代碼開始:

Sub Import() 
    Dim SourceFile As Workbook 
    Dim SourceTab As Worksheet 
    Dim TargetTab As Worksheet 

    SourceFileName = Application.GetOpenFilename("Excel Files ,  *.xlt;*.xls;*.xlsx;*.csv") 

    If SourceFileName = False Then Exit Sub 
    Application.ScreenUpdating = False 

    Set TargetTab = Sheets("Output") 
    TargetRow = TargetTab.Cells(TargetTab.Cells.Rows.Count, 3).End(xlUp).Row + 1 

    Set SourceFile = Workbooks.Open(SourceFileName) 

    SourceFile.Activate 
    Set SourceTab = Sheets("Sheet1") 
    SourceTab.Activate 

    For i = 1 To Cells(Cells.Rows.Count, 2).End(xlUp).Row 

     If Len(Cells(i, 2).Value) = 2 Then 

      Cells(i, 3).Value = Cells(i, 31).Value 
      Cells(i, 31).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 3).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 5).Value = Cells(i, 11).Value 
      Cells(i, 11).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 5).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 6).Value = Cells(i, 19).Value 
      Cells(i, 19).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 6).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 7).Value = Cells(i, 27).Value 
      Cells(i, 27).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 7).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 9).Value = Cells(i, 4).Value 
      Cells(i, 4).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 9).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 11).Value = Cells(4, 5).Value 
      Cells(4, 5).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 11).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 13).Value = Cells(2, 25).Value 
      Cells(2, 25).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 13).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 14).Value = Cells(i, 43).Value 
      Cells(i, 43).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 14).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      Cells(i, 17).Value = Cells(i, 8).Value 
      Cells(i, 8).Resize(1, 1).Copy 
      ThisWorkbook.Activate 
      TargetTab.Activate 
      Cells(TargetRow, 17).Select 
      Selection.PasteSpecial Paste:=xlPasteValues 
      Application.CutCopyMode = False 
      SourceFile.Activate 

      TargetRow = TargetRow + 1 
      'TargetNewRows = TargetNewRows + 1 
     End If 
    Next 
    SourceFile.Close False 
    Application.ScreenUpdating = True 
    MsgBox "Done" 
End Sub 
+0

我不確定這應該需要30秒在Excel 2007中...我會開始使用工作表和工作簿引用和直接分配,而不是.Activate和全局對象。 – Comintern

+0

先生...坦率地說...我對VBA很新...根本沒有得到你的答案...因此,請你讓我知道確切的地方,我需要做出什麼樣的改變.. –

+0

任何人,請你幫我解決這個問題......它非常緊迫 –

回答

0

這種類型的事情:

Cells(i, 6).Value = Cells(i, 19).Value 
    Cells(i, 19).Resize(1, 1).Copy 
     ThisWorkbook.Activate 
     TargetTab.Activate 
     Cells(TargetRow, 6).Select 
     Selection.PasteSpecial Paste:=xlPasteValues 
     Application.CutCopyMode = False 
     SourceFile.Activate 

會更好,如:

SourceTab.Cells(i, 6).Value = SourceTab.Cells(i, 19).Value 
TargetTab.Cells(TargetRow, 6).Value = SourceTab.Cells(i, 19).Value 

跳過所有的選擇/激活調用,這是不併且使您的代碼可能意外失敗。您的代碼應儘可能不要依賴任何特定的活動/選擇的範圍,圖紙或工作簿。

相關問題