我正在使用此代码将已关闭工作簿中的工作表中的所有数据复制到目标工作簿。但是,此代码循环遍历每个单元格,因为源工作簿包含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
答案 0 :(得分: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