操作/复制.CSV数据,而不打开文件?

我试图优化一些代码,它将一些testing数据存储在CSV文件中进行一些分析,并将其数据复制到Excel表格中。 此代码通常每次运行数百个testing,每次testing需要花费大约4.5秒的时间,因此可能需要几个小时才能完成。

我查了一些优化技术,并在每次testing中减less了大约0.25秒,但是我认为大部分时间都是由excel占用的,因为excel必须“打开”各个文件才能对它们做任何事情。 有没有办法更有效地做到这一点?

我很乐意接受使用另一种语言将文件编译成一个大文件的答案,如果这样做会使事情变得更快。

我会打开他们作为文本,而不是工作簿:

 Sub ReadCSV() Dim MyString As String Open "C:\path\text.csv" For Input As #1 ' Open file for input. Do While Not EOF(1) ' Loop until end of file. Line Input #1, MyString ' Read a line into variable Debug.Print MyString ' Print data to the Immediate window. Loop Close #1 ' Close file. End Sub 

这将比打开工作簿快得多

我有这个function工作处理大量的CSV文件。 您需要在单元格“D11”中指定包含所有CSV文件的文件夹的名称,并将它们合并为一个文件。 我处理200多个文件,并使其快速。 希望能帮助到你。

 Sub CombineAllFilesInADirectory() Dim Path As String 'string variable to hold the path to look through Dim FileName As String 'temporary filename string variable Dim tWB As Workbook 'temporary workbook (each in directory) Dim tWS As Worksheet 'temporary worksheet variable Dim aWS As Worksheet 'active sheet in master workbook Dim RowCount As Long 'Rows used on master sheet Dim uRange As Range 'usedrange for each temporary sheet Dim mWB_comb As Workbook 'master workbook exclusivo de esta funcion Path = Sheets("CombineFiles").Range("D11").Value Application.EnableEvents = False 'turn off events Application.ScreenUpdating = False 'turn off screen updating Set mWB_comb = Workbooks.Add(1) 'create a new one-worksheet workbook Set aWS = mWB_comb.ActiveSheet 'set active sheet variable to only sheet in mWB If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\" Path = Path & Application.PathSeparator 'add "\" End If FileName = Dir(Path & "*.csv", vbNormal) 'set first file's name to filename variable Application.StatusBar = "reading files, please wait." Do Until FileName = "" 'loop until all files have been parsed If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable For Each tWS In tWB.Worksheets 'loop through each sheet Set uRange = tWS.Range("A4", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows.count - 1, _ tWS.UsedRange.Column + tWS.UsedRange.Columns.count - 1)) 'set used range If RowCount + uRange.Rows.count > 65536 Then 'if the used range wont fit on the sheet aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns Set aWS = mWB_comb.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data RowCount = 0 'reset RowCount variable End If If RowCount = 0 Then 'if working with a new sheet aWS.Range("A1", aWS.Cells(3, uRange.Columns.count)).Value = tWS.Range("A1", _ tWS.Cells(3, uRange.Columns.count)).Value 'copy headers from tWS RowCount = 3 'add one to rowcount End If aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.count, _ uRange.Columns.count).Value = uRange.Value 'move data from temp sheet to data sheet RowCount = RowCount + uRange.Rows.count 'increase rowcount accordingly Next 'tWS tWB.Close False 'close temporary workbook without saving End If FileName = Dir() 'set next file's name to FileName variable Loop Application.StatusBar = "Ready" mWB_comb.Sheets(1).Select 'select first data sheet on master workbook Application.EnableEvents = True 're-enable events Application.ScreenUpdating = True 'turn screen updating back on 'Clear memory of the object variables Set tWB = Nothing Set tWS = Nothing Set mWB_comb = Nothing Set aWS = Nothing Set uRange = Nothing End Sub