Optimization 是否在不打开文件的情况下操作/复制.CSV数据?

Optimization 是否在不打开文件的情况下操作/复制.CSV数据?,optimization,excel,vba,Optimization,Excel,Vba,我正在尝试优化一些代码,这些代码将一些存储在CSV文件中的测试数据进行分析,并将它们的数据复制到excel工作表中。此代码通常一次在数百个测试上运行,每个测试大约需要4.5秒,因此有时可能需要数小时才能完成 我查阅了一些优化技术,每次测试大约减少了.25秒,但我认为大部分时间都被excel占用了,excel必须先“打开”单个文件,然后才能处理它们。有没有更有效的方法 我愿意接受涉及使用另一种语言将文件编译成一个大文件的答案,如果这能使事情更快的话。我会将它们作为文本而不是工作簿打开: Sub R

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

我查阅了一些优化技术,每次测试大约减少了.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

这将比以工作簿的形式打开快得多

我将以文本而不是工作簿的形式打开它们:

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

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

我有这个功能,可以处理大量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
子组合alfilesinadirectory()
Dim Path As String'字符串变量,用于保存要查看的路径
Dim FileName As String临时文件名字符串变量
将tWB作为工作簿的临时工作簿(每个工作簿位于目录中)
Dim tWS作为工作表的临时工作表变量
在主工作簿中将aWS设置为工作表的活动工作表
将行数调整为母版图纸上使用的“长”行
Dim保险作为每个临时表的使用范围
Dim mWB_comb作为工作簿的主工作簿,独家使用esta功能
路径=图纸(“组合文件”).范围(“D11”).值
Application.EnableEvents=False“关闭事件”
Application.ScreenUpdate=False“关闭屏幕更新”
设置mWB_comb=工作簿。添加(1)“新建一个工作表工作簿”
设置aWS=mWB_comb.ActiveSheet“将活动工作表变量设置为仅mWB中的工作表
如果正确(路径,1)Application.PathSeparator,则“如果路径未以“\”结尾”
Path=Path&Application.PathSeparator'添加“\”
如果结束
FileName=Dir(Path&“*.csv”,vbNormal)”将第一个文件的名称设置为FileName变量
Application.StatusBar=“正在读取文件,请稍候。”
Do Until FileName=“”循环,直到所有文件都已解析
如果路径为ThisWorkbook.Path或文件名为ThisWorkbook.Name,则
设置tWB=Workbooks.Open(FileName:=Path&FileName)“打开文件,设置为tWB变量
对于tWB中的每个TW。工作表在每个工作表中循环
设置uRange=tWS.范围(“A4”,tWS.单元格(tWS.UsedRange.Row+tWS.UsedRange.Rows.count-1_
tWS.UsedRange.Column+tWS.UsedRange.Columns.count-1))'设置使用范围
如果RowCount+urage.Rows.count>65536,则“如果使用的范围不适合工作表
aWS.Columns.AutoFit“AutoFit主要用于工作表的列
设置aWS=mWB_comb.Sheets.Add(在:=aWS之后)'添加一个将容纳数据的新工作表
RowCount=0'重置RowCount变量
如果结束
如果RowCount=0,则“如果使用新工作表
aWS.Range(“A1”,aWS.Cells(3,uRange.Columns.count)).Value=tWS.Range(“A1”_
tWS.Cells(3,uRange.Columns.count)).Value'从tWS复制头
RowCount=3'将一个添加到RowCount
如果结束
aWS.Range(“A”&行数+1)。调整大小(urage.Rows.count_
Urage.Columns.count).Value=Urage.Value'将数据从临时工作表移动到数据表
RowCount=RowCount+urage.Rows.count'相应增加RowCount
下一个tWS
tWB.Close False“关闭临时工作簿而不保存”
如果结束
FileName=Dir()'将下一个文件的名称设置为FileName变量
环
Application.StatusBar=“就绪”
mWB_组合表(1)。选择“选择主工作簿上的第一个数据表”
Application.EnableEvents=True“重新启用事件”
Application.ScreenUpdate=True“重新打开屏幕更新”
'清除对象变量的内存
设置tWB=无
设置tWS=无
设置mWB_梳=无
设置aWS=无
设定保险费=零
端接头

我使用此功能处理大量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
子组合alfilesinadirectory()
Dim Path As String'字符串变量,用于保存要查看的路径
Dim FileName As String临时文件名字符串变量
将tWB作为工作簿的临时工作簿(每个工作簿位于目录中)
Dim tWS作为工作表的临时工作表变量
在主工作簿中将aWS设置为工作表的活动工作表
将行数调整为母版图纸上使用的“长”行
Dim保险作为每个临时表的使用范围
Dim mWB_comb作为工作簿的主工作簿,独家使用esta功能
路径=图纸(“组合文件”).范围(“D11”).值
Application.EnableEvents=False“关闭事件”
Application.ScreenUpdate=False“关闭屏幕更新”
设置mWB_comb=工作簿。添加(1)“新建一个工作表工作簿”
设置aWS=mWB_comb.ActiveSheet“将活动工作表变量设置为仅mWB中的工作表
如果正确(路径,1)Application.PathSeparator,则“如果路径未以“\”结尾”
Path=Path&Application.PathSeparator'添加“\”
如果结束
FileName=Dir(Path&“*.csv”,vbNormal)”将第一个文件的名称设置为FileName变量
Application.StatusBar=“正在读取文件,p