gpt4 book ai didi

arrays - 将 CSV 文件的内容加载到数组而不打开文件

转载 作者:行者123 更新时间:2023-12-04 20:00:36 25 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .


5年前关闭。







Improve this question




我需要将 6000 多个 csv 文件整理成一个 csv 文档。当前的VBA流程是:
1. 打开单个 CSV 数据文件
2.根据行数将文件内容加载到数组中
3. 关闭单个 CSV 文件
4.进程数组

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

我正在使用 Excel 2011 for Mac。

最佳答案

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

我的测试条件

  • 我有一个名为 C:\Temp\的文件夹,其中包含 6000 个 CSV 文件
  • 所有 csv 文件都有 40 行和 16 列
  • 在 Excel 2010 中对其进行了测试。无法访问 2011。将在 2011 年大约 30 分钟内对其进行测试。

  • 我运行了下面的代码,代码只用了 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

    更新

    好的,我在 MAC 中测试过

    我的测试条件
  • 我在桌面上有一个名为 Sample 的文件夹,其中包含 1024 个 CSV 文件
  • 所有 csv 文件都有 40 行和 16 列
  • 在 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

    关于arrays - 将 CSV 文件的内容加载到数组而不打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907952/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com