0

我正在尝试优化一些代码,这些代码将一些存储在 CSV 文件中的测试数据进行一些分析并将它们的数据复制到 Excel 表中。此代码通常一次运行数百个测试,每次测试大约需要 4.5 秒,因此有时可能需要数小时才能完成。

我查找了一些优化技术,并将每次测试的时间缩短了约 0.25 秒,但我认为大部分时间都被 excel 占用了,它必须先“打开”单个文件,然后才能对它们做任何事情。有没有办法更有效地做到这一点?

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

4

2 回答 2

3

我会将它们作为文本而不是工作簿打开:

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

这将比作为工作簿打开要快得多

于 2012-05-24T10:44:54.423 回答
1

我有这个功能可以处理大量的 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
于 2012-05-24T16:24:22.200 回答