Bes*_*ion 0 csv excel ms-access vba excel-vba
嗨,所以我完成了一个程序部分,计算和导出带有结果的csv.(最终约1600个csv文件),每个文件只有1列,在20到0行之间.我希望我的MS Access VBA程序将它们连接成一个更大的CSV.所以相同的标题只在新文件的顶部一次.
我到目前为止的程序似乎在它试图导入Reg的部分失败了.文件号.
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
Run Code Online (Sandbox Code Playgroud)
你的问题对于你正在尝试做的事情并不是绝对明确的,但如果我理解正确,你只需要将几个文件附加到彼此的末尾,以制作"一个大的CSV".
如果这是真的那么有几种方法比使用VBA简单得多. .CSV文件只是纯文本文件,逗号分隔每个字段,.CSV文件扩展名.
就个人而言,我会使用Notepad ++(我认为它能够做到这一点;它会做其他所有事情),或者甚至更简单,我会使用Windows 命令提示符.
假设您有一个包含文件的文件夹:
File1.csv
File2.csv
File3.csv
...etc
Run Code Online (Sandbox Code Playgroud)
打开Windows命令提示符.(一种方法是使用
Windows键+ R,然后键入cmd并按Enter键.)
使用cd(相同ChDir)将目录更改为文件位置.
(例如,您可以使用cd c:\users\myFolder,然后点击Enter)
要将CSV文件夹中的所有内容合并为一个,您可以使用如下命令:
copy *.csv combinedfile.csv
Run Code Online (Sandbox Code Playgroud)而已!
创建一个名为的文件combinedfile.csv.您可以在Excel或文本编辑器(如记事本)中打开以仔细检查并在必要时手动调整.
显然,有很多方法可以改变命令,比如你只想要以File你可以使用的单词开头的文件:
copy file*.csv combinedFile.csv
Run Code Online (Sandbox Code Playgroud)