2013-02-16 27 views
2

我有一个要求将6000个csv文件整理到一个csv文档中。当前VBA过程是: 1.打开个人CSV数据文件基于 4.流程阵列将CSV文件的内容加载到阵列而不打开文件

为了提高效率的行数 3.关闭个别CSV文件文件到阵列的 2.加载内容代码和处理,我希望有一种方法可以将单个CSV文件中的数据加载到数组中,而无需打开和关闭每个文件。

我使用Excel 2011 for Mac。

+3

也许只是'猫<所有的CSV文件>> one_big_csv_file'' – nneonneo 2013-02-16 07:16:15

+0

是否所有的csv文件都采用相同的格式? – 2013-02-16 11:14:23

+0

是的,所有的CSV格式都是相同的格式,尽管每个文件中有不同数量的行。 – 2013-02-18 14:48:38

回答

0

无论如何,在我看来,没有Excel的答案可以解决您的问题 - 当然不在其正常的定义范围内。

解决它的正确方法是使用适合任务的编程语言;例如perl,甚至是命令shell,来合并这些文件。 Excel不是用于常量文件I/O,但是Perl在处理大量文件方面非常出色。我在几分钟内在一个相对较小的unix服务器上执行了一个类似于此的项目(合并数百万个文件)。

您也可以使用命令shell将文件一起捕获(cat = concatenate),如nneonneo在注释中所示;我不能说哪个更快。 Perl肯定会花费更长的时间来编写代码,特别是如果你必须首先学习perl(尽管网上有很多例子)。

3

好吧,我假设所有6000文件具有相同的格式。

我的测试条件

  1. 我有一个文件夹名为C:\ TEMP \其中有6000个CSV文件
  2. 所有CSV文件有40行和16列
  3. 在Excel 2010中唐测试它没有进入2011年。将在大约30分钟的2011年进行测试。

我跑了下面的代码,代码只用了4秒钟。

Option Explicit 

Sub Sample() 
    Dim strFolder As String, strFile As String 
    Dim MyData As String, strData() As String 
    Dim FinalArray() As String 
    Dim StartTime As String, endTime As String 
    Dim n As Long, j As Long, i As Long 

    strFolder = "C:\Temp\" 

    strFile = Dir(strFolder & "*.csv") 

    n = 0 

    StartTime = Now 

    Do While strFile <> "" 
     Open strFolder & strFile For Binary As #1 
     MyData = Space$(LOF(1)) 
     Get #1, , MyData 
     Close #1 

     strData() = Split(MyData, vbCrLf) 
     ReDim Preserve FinalArray(j + UBound(strData) + 1) 
     j = UBound(FinalArray) 

     For i = LBound(strData) To UBound(strData) 
      FinalArray(n) = strData(i) 
      n = n + 1 
     Next i 

     strFile = Dir 
    Loop 

    endTime = Now 

    Debug.Print "Process started at : " & StartTime 
    Debug.Print "Process ended at : " & endTime 
    Debug.Print UBound(FinalArray) 
End Sub 

截图的文件夹的

enter image description here

截图的代码输出

enter image description here


UPDATE

好吧,我在MAC

测试它

我的测试条件

  1. 我有一个名为其中有1024个CSV文件
  2. 所有CSV文件有40个桌面上的示例文件夹行和16列
  3. 在Excel 2011中测试它。

我运行了下面的代码,代码花费的时间不到1秒(因为只有1024个文件)。因此,我期待它的情况下为4秒再次运行有6K文件

Sub Sample() 
    Dim strFile As String 
    Dim MyData As String, strData() As String 
    Dim FinalArray() As String 
    Dim StartTime As String, endTime As String 
    Dim n As Long, j As Long, i As Long 

    StartTime = Now 

    MyDir = ActiveWorkbook.Path 
    strPath = MyDir & ":" 

    strFile = Dir(strPath, MacID("TEXT")) 

    'Loop through each file in the folder 
    Do While Len(strFile) > 0 
     If Right(strFile, 3) = "csv" Then 
      Open strFile For Binary As #1 
      MyData = Space$(LOF(1)) 
      Get #1, , MyData 
      Close #1 

      strData() = Split(MyData, vbCrLf) 
      ReDim Preserve FinalArray(j + UBound(strData) + 1) 
      j = UBound(FinalArray) 

      For i = LBound(strData) To UBound(strData) 
       FinalArray(n) = strData(i) 
       n = n + 1 
      Next i 

      strFile = Dir 
     End If 
     strFile = Dir 
    Loop 

    endTime = Now 

    Debug.Print "Process started at : " & StartTime 
    Debug.Print "Process ended at : " & endTime 
    Debug.Print UBound(FinalArray) 
End Sub 

截图文件夹的

enter image description here

截图编码输出的

enter image description here

+0

谢谢Siddharth,这看起来是一个非常全面的答案,我会测试和回复。不胜感激! – 2013-02-18 14:50:45

+0

嗨Siddharth,我测试了建议的代码,并且从未输入循环,因为strFile具有值“”。工作簿保存在相关CSV文件的文件夹中。 – 2013-02-20 23:04:05

0

你不需要使用Excel来做到这一点,你可以使用Windows从命令提示符复制输入合并:

copy *.csv mergedfilename.csv 
+0

它也会复制标题。 – 2017-10-23 13:21:47

相关问题