我正在尝试将.csv文件复制到单个.xlsx文件中。但是,文件相当大(400,000行),几秒钟后我得到运行时错误1004.
我的复制文件的代码如下。假设通过在写入过程中定期保存文件,这个错误应该被修复,但我不知道该怎么做。将每个文件放在自己的工作表中会更好吗?
Dim x As Variant
Dim Cnt As Long, r As Long, c As Long
FilePath = Application.ActiveWorkbook.Path & "\"
file = Dir(FilePath & "*.csv")
Do While Len(file) > 0
Cnt = Cnt + 1
r = Cells(Rows.Count, "A").End(xlUp).Row + 1
Open FilePath & file For Input As #1
Do Until EOF(1)
Line Input #1, strData
x = Split(strData, ",")
For c = 0 To UBound(x)
Cells(r, c + 1).Value = Trim(x(c))
Next c
r = r + 1
Loop
Close #1
file = Dir
Loop
If Cnt = 0 Then MsgBox "No CSV files found...", vbExclamation
它在行上提供错误:Cells(r, c + 1).Value = Trim(x(c))
此代码似乎将所有行复制到输出文件中的同一行,并且当它达到最大列数时停止。 (.csv文件是32列。)