English 中文(简体)
处理/复制. CSV 数据, 不打开文件吗?
原标题:Manipulate/copy .CSV data, without opening the file?

我试图优化一些需要CSV文档中储存的一些测试数据的代码, 做一些分析并将其数据复制到一个优秀的表格中。这个代码经常一次运行在数百个测试上,每次测试花费大约4.5秒,这样它可以花小时完成。

我查了一些优化技术,每次测试大约削减了25秒,但我认为大部分时间都用在了优秀的人必须先“打开”个人文件,然后才能用它们做任何事情。 是否有办法更有效地做这件事?

使用另一种语言将文件编成一个大文件,

最佳回答

我将把它们作为文本而不是工作手册来打开:

Sub ReadCSV()
    Dim MyString As String
    Open "C:path	ext.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

这将比作为工作手册打开要快很多 < / em > 。

问题回答

我有一个功能可以很好地处理很多 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




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签