Excel只粘贴了我范围的一部分,其余部分则出错.我该如何解决这个问题?



我正在构建一个宏,允许用户选择一个文件夹,选择要导入的选项卡,然后它会打开文件夹中的每个文件,抓取给定选项卡中的所有数据,并将其导入到一个主表中以便于查看。

它以一种有趣的方式中断 - 当它用行粘贴数据时

DestinationWorkbook.Sheets("Data").Range("A" & LastrowOutput).Resize(DataRng.Rows.Count, DataRng.Columns.Count).Value = DataRng.Value

它发布在某些数据中,然后生成错误消息"运行时错误'1004':应用程序定义或对象定义的错误" 有关细节,在我的第一个文件中,我有 15311 行 DD 列。它在生成错误消息时导入 14832 行。

(使用较少的列进行测试(

此外,在调试和单步执行时,它会从 If 语句直接跳转到问题,同时生成问题和所有数据。

在给定的行的数据中没有任何异常。

我是否达到了一些技术限制?有谁知道发生了什么?我的谷歌福让我失望了。

编辑:当使用选项卡中的数据再次运行代码时,我设法在收到"自动化错误"错误消息之前到达第 29663 行。仍在进行该列测试...

编辑2:让它只选择有问题的列。仍然在同一行中断。

编辑 3:分解代码以执行前 10,000 行,然后执行所有剩余行。仍然在同一行上中断。有人建议我寻找隐藏的行和列 - 没有隐藏的行或列。接下来将尝试值 2 和值 3。

Sub PullingAllData()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim sPath As String
Dim sFil As String
Dim FolderPath As String
Dim diaFolder As FileDialog
Dim DestinationWorkbook As Workbook
Dim DataRng As Range
Dim LastrowInput As Double
Dim LastrowOutput As Double

'Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
FolderPath = diaFolder.SelectedItems(1)
' Cycle through spreadsheets in selected folder


Set DestinationWorkbook = ActiveWorkbook
Dim ImportTabNumber As Integer
ImportTabNumber = InputBox("Please Enter the tab to import.*", "Account Name")

sPath = FolderPath & "" 'location of files
sFil = Dir(sPath & "*.xlsx") 'change or add formats
Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped            through

Set owbk = Workbooks.Open(sPath & "" & sFil) 'opens the file
If owbk.Sheets.Count >= ImportTabNumber Then
Set InputTab = owbk.Sheets(ImportTabNumber)
LastrowInput = InputTab.Range("A" & Rows.Count).End(xlUp).Row
Set DataRng = InputTab.Range("A1:DO" & LastrowInput).SpecialCells(xlCellTypeVisible)

LastrowOutput = DestinationWorkbook.Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1
DestinationWorkbook.Sheets("Data").Range("A" & LastrowOutput).Resize(DataRng.Rows.Count, DataRng.Columns.Count).Value = DataRng.Value
End If

owbk.Close True
sFil = Dir
Loop
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

这篇文章(特别是底部附近的Cees Timmerman的回答(可能会对这个问题有所了解。还值得检查您是否拥有 32 位或 64 位版本的 Excel。如果您有前者,则可能内存不足。

如果您有后者,请尝试创建一个Variant数组来存储数据,然后再传输数据(Dim MyData as Variant : MyData = MyRange.Value(。

我还建议打开任务管理器并检查性能选项卡。您应该能够使用资源监视器来跟踪正在使用的 RAM 量。如果您确实拥有 64 位,RAM 不太可能成为问题的根源,但看看幕后发生的事情(以及该操作的成本(并没有什么坏处。

在将该代码投入生产之前,我强烈建议进行更多测试(使用更多文件(。如果该代码已经中断,则将来很可能会再次中断。

最新更新