通过MS Acces s vba将一个文件夹中的CSV文件合并到一个文件中

通过MS Acces s vba将一个文件夹中的CSV文件合并到一个文件中,vba,excel,csv,ms-access,Vba,Excel,Csv,Ms Access,大家好,所以我完成了一个程序的一部分,该程序计算并导出一个csv和结果。(最终得到大约1600个csv文件)每个文件只有一列,并且在20到0行之间。我希望我的MS Access VBA程序将它们合并到一个更大的CSV中。因此,同一个头在新文件的顶部仅出现一次 到目前为止,我的程序似乎在试图导入注册表的部分失败了。文件的编号 Dim db As DAO.Database Set db = CurrentDb MTH = Format(Date, "mmm") UserInput = InputBo

大家好,所以我完成了一个程序的一部分,该程序计算并导出一个csv和结果。(最终得到大约1600个csv文件)每个文件只有一列,并且在20到0行之间。我希望我的MS Access VBA程序将它们合并到一个更大的CSV中。因此,同一个头在新文件的顶部仅出现一次

到目前为止,我的程序似乎在试图导入注册表的部分失败了。文件的编号

Dim db As DAO.Database
Set db = CurrentDb
MTH = Format(Date, "mmm")
UserInput = InputBox("Enter Country Code")
Dim strSourcePath As String
Dim strDestPath As String
Dim strFile As String
Dim strData As String
Dim x As Variant
Dim Cnt As Long
Dim r As Long
Dim c As Long
Dim wks As Excel.Worksheet

    Application.Echo False

    'Change the path to the source folder accordingly
    strSourcePath = "Q:\CCNMACS\AWD" & CTRY

    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"

    'Change the path to the destination folder accordingly
    strDestPath = "Q:\CCNMACS\AWDFIN"

    If Right(strDestPath, 1) <> "\" Then strDestPath = strDestPath & "\"

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

  Do While Len(strFile) > 0
        Cnt = Cnt + 1
        If Cnt = 1 Then
            r = 1
        Else
            r = Cells(Rows.Count, "A").End(xlUp).Row + 1
        End If
        Open strSourcePath & strFile For Input As #1
            If Cnt > 1 Then
                Line Input #1, strData
            End If
            Do Until EOF(1)
                Line Input #1, strData
                x = Split(strData, ",")
                For c = 0 To UBound(x)
                    wks.Cells(r, c + 1).Value = Trim(x(c)) 'Error is here: Run time error '91': Object variable or With Block variable not set
                Next c
                r = r + 1
            Loop
        Close #1
        Name strSourcePath & strFile As strDestPath & strFile
        strFile = Dir
    Loop

    Application.Echo True

    If Cnt = 0 Then _
        MsgBox "No CSV files were found...", vbExclamation
Dim db作为DAO.Database
Set db=CurrentDb
MTH=格式(日期,“mmm”)
用户输入=输入框(“输入国家代码”)
将strSourcePath设置为字符串
作为字符串的Dim strdespath
作为字符串的Dim strFile
将strData设置为字符串
Dim x作为变体
暗淡的碳纳米管
变暗,变长
尺寸c与长度相同
Dim以Excel格式工作。工作表
应用程序。回显错误
'相应地更改源文件夹的路径
strSourcePath=“Q:\CCNMACS\AWD”&CTRY
如果正确(strSourcePath,1)“\”则strSourcePath=strSourcePath&“\”
'相应地更改目标文件夹的路径
strDestPath=“Q:\CCNMACS\AWDFIN”
如果正确(strDestPath,1)“\”则strDestPath=strDestPath&“\”
strFile=Dir(strSourcePath&“*.csv”)
当Len(strFile)>0时执行
Cnt=Cnt+1
如果Cnt=1,则
r=1
其他的
r=单元格(行数,“A”)。结束(xlUp)。行数+1
如果结束
打开strSourcePath&strFile以作为#1输入
如果Cnt>1,则
行输入#1,标准数据
如果结束
直到EOF(1)为止
行输入#1,标准数据
x=拆分(标准数据,“,”)
对于c=0到UBound(x)
wks.Cells(r,c+1).Value=Trim(x(c))'此处有错误:运行时错误'91':未设置对象变量或带块变量
下一个c
r=r+1
环
关闭#1
将strSourcePath和strFile命名为strdespath和strFile
strFile=Dir
环
应用程序。Echo True
如果Cnt=0,则_
MsgBox“未找到CSV文件…”,请感叹

你的问题对于你想做什么并不是绝对确定的,但如果我理解正确,你只需要在每个文件的末尾附加几个文件,就可以创建“一个大CSV”

如果这是真的,那么有几种方法比使用VBA简单得多
.CSV
文件只是纯文本文件,每个字段之间用逗号分隔,文件扩展名为
.CSV

就我个人而言,我会使用(我认为它能够做到这一点;它可以做其他一切),或者更简单,我会使用Windows命令提示符


假设您有一个包含文件的文件夹:

File1.csv
File2.csv
File3.csv
...etc
  • 打开Windows命令提示符。(一种方法是使用Windows键+R,然后键入
    cmd
    并按Enter键。)

  • 使用
    cd
    (与
    ChDir
    相同)将目录更改为文件位置。
    (例如,您可以使用
    cd c:\users\myFolder
    , 然后按回车键)

  • 要将文件夹中的所有
    CSV
    合并为一个,可以使用如下命令:

    copy *.csv combinedfile.csv
    
就这样


将创建一个名为
combinedfile.csv
的文件。您可以在Excel或文本编辑器(如记事本)中打开以进行双重检查,并在必要时手动调整

显然,有许多方法可以改变命令,例如,如果您只需要以单词
File
开头的文件,您可以使用:

copy file*.csv combinedFile.csv

你的问题对于你想做什么并不是绝对确定的,但如果我理解正确,你只需要在每个文件的末尾附加几个文件,就可以创建“一个大CSV”

如果这是真的,那么有几种方法比使用VBA简单得多
.CSV
文件只是纯文本文件,每个字段之间用逗号分隔,文件扩展名为
.CSV

就我个人而言,我会使用(我认为它能够做到这一点;它可以做其他一切),或者更简单,我会使用Windows命令提示符


假设您有一个包含文件的文件夹:

File1.csv
File2.csv
File3.csv
...etc
  • 打开Windows命令提示符。(一种方法是使用Windows键+R,然后键入
    cmd
    并按Enter键。)

  • 使用
    cd
    (与
    ChDir
    相同)将目录更改为文件位置。
    (例如,您可以使用
    cd c:\users\myFolder
    , 然后按回车键)

  • 要将文件夹中的所有
    CSV
    合并为一个,可以使用如下命令:

    copy *.csv combinedfile.csv
    
就这样


将创建一个名为
combinedfile.csv
的文件。您可以在Excel或文本编辑器(如记事本)中打开以进行双重检查,并在必要时手动调整

显然,有许多方法可以改变命令,例如,如果您只需要以单词
File
开头的文件,您可以使用:

copy file*.csv combinedFile.csv

这应该是你想要的

Sub Import()

        Dim strPathFile As String, strFile As String, strPath As String
        Dim strTable As String
        Dim blnHasFieldNames As Boolean

        ' Change this next line to True if the first row in EXCEL worksheet
        ' has field names
        blnHasFieldNames = True

        ' Replace C:\Documents\ with the real path to the folder that
        ' contains the EXCEL files
        strPath = "C:\your_path_here\"

        ' Replace tablename with the real name of the table into which
        ' the data are to be imported
        strTable = "Table1"

        strFile = Dir(strPath & "*.csv")
        Do While Len(strFile) > 0
              strPathFile = strPath & strFile
              DoCmd.TransferText acImportDelim, "", strTable, strPathFile, blnHasFieldNames

        ' Uncomment out the next code step if you want to delete the
        ' EXCEL file after it's been imported
        '       Kill strPathFile

              strFile = Dir()
        Loop

End Sub

有关此主题的其他详细信息,请参阅下面的链接


这应该是你想要的

Sub Import()

        Dim strPathFile As String, strFile As String, strPath As String
        Dim strTable As String
        Dim blnHasFieldNames As Boolean

        ' Change this next line to True if the first row in EXCEL worksheet
        ' has field names
        blnHasFieldNames = True

        ' Replace C:\Documents\ with the real path to the folder that
        ' contains the EXCEL files
        strPath = "C:\your_path_here\"

        ' Replace tablename with the real name of the table into which
        ' the data are to be imported
        strTable = "Table1"

        strFile = Dir(strPath & "*.csv")
        Do While Len(strFile) > 0
              strPathFile = strPath & strFile
              DoCmd.TransferText acImportDelim, "", strTable, strPathFile, blnHasFieldNames

        ' Uncomment out the next code step if you want to delete the
        ' EXCEL file after it's been imported
        '       Kill strPathFile

              strFile = Dir()
        Loop

End Sub

有关此主题的其他详细信息,请参阅下面的链接


您需要分享一个数据示例,以及数据的组合方式。如果我正确理解了您需要做的事情(即将多个文件附加到一个“长”文件中),那么在没有VBA的情况下,有几种方法可以做到这一点。仅今天就有3次这样的问题。因此,该文件非常基本,只是在A列中的标题为“Reg.Number”在此情况下,您将得到20到0个数字条目,最多9个字符long@Jeeped我一直在寻找类似于我的问题的东西,但我只能找到人们在Dos、python和R中这样做的例子