0

我正在使用此代码将所有数据从已关闭工作簿中的工作表复制到目标工作簿。但是,此代码循环遍历每个单元格,并且由于源工作簿包含 40,000 行,因此花费的时间太长。可以对此代码进行哪些更改以将所有数据从源工作表复制到目标工作簿中的特定工作表,而无需遍历单元格,或者如果您可以提供替代解决方案。感谢您的帮助

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 
4

1 回答 1

0

我在链接上为您找到了圆顶好/类似的解决方案:

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
于 2013-12-13T12:18:22.400 回答