2013-10-28 112 views
0

我正在使用此代碼將所有數據從關閉的工作簿中的工作表中複製到目標工作簿。但是,此代碼循環遍歷每個單元格,並且由於源工作簿包含40,000行,因此其耗時過長。什麼樣的變化可以對這個代碼進行從源工作表中的所有數據複製到目標工作簿的特定工作表,而無需通過細胞循環,或者如果有一個替代的解決方案,你可以provide.Thanks的幫助將大型數據從已關閉的工作簿複製到目標工作簿中的特定工作表

Sub GetDataDemo() 

Dim FilePath$, Row&, Column&, Address$ 

Const FileName$ = "Book1.xls" 
Const SheetName$ = "Sheet1" 
Const NumRows& = 40000 
Const NumColumns& = 10 
FilePath = ActiveWorkbook.Path & "\" 
'*************************************** 
    DoEvents 
Application.ScreenUpdating = False 
If Dir(FilePath & FileName) = Empty Then 
    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist" 
    Exit Sub 
End If 
For Row = 1 To NumRows 
    For Column = 1 To NumColumns 
     Address = Cells(Row, Column).Address 
     dest. Cells(Row, Column) = GetData(FilePath, FileName, SheetName, Address) 
     Columns.AutoFit 
    Next Column 
Next Row 
ActiveWindow.DisplayZeros = False 
End Sub 


Private Function GetData(Path, File, Sheet, Address) 
    Dim Data$ 
    Data = "'" & Path & "[" & File & "]" & Sheet & "'!" & _ 
    Range(Address).Range("A1").Address(, , xlR1C1) 
    GetData = ExecuteExcel4Macro(Data) 
End Function 
+0

也許[此](http://stackoverflow.com/questions/16981081/executeexcel4macro-to-get-range-charts-from-closed-workbooks)發佈是你需要什麼?我沒有辦法比較執行時間。這篇文章不使用'ExecuteExcel4Macro'就從'Range'中的封閉工作表中提取數據。 – L42

+2

你有沒有試過打開封閉的工作簿?打開已關閉的工作簿並複製整個範圍而不循環 –

+0

@ l42 ..可能會更快。感謝範圍公式工作正常。 – vik

回答

0

我有founs穹頂好/相似sollution爲你鏈接:

http://social.msdn.microsoft.com/Forums/office/en-US/aa6edf0f-2007-4f25-885c-f03d3df4d853/pulling-values-from-closed-workbook-into-current-workbook-but-on-a-different-sheet 

在你的情況下更換:

For Row = 1 To NumRows 
For Column = 1 To NumColumns 
Address = Cells(Row, Column).Address 
dest. Cells(Row, Column) = GetData(FilePath, FileName, SheetName, Address) 
Columns.AutoFit 
Next Column 
Next Row 

帶:

For Row = 1 To NumRows 
For Column = 1 To NumColumns 
Address = Cells(Row, Column).Address 
Cells(Row, Column + X) = GetDataEleven(FilePath, FileName, SheetName, Address) 
Next Column 
Next Row 
Columns.AutoFit 
相關問題