【发布时间】:2018-07-30 11:41:03
【问题描述】:
您好,我完成了计算和导出带有结果的 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
【问题讨论】:
-
您需要分享一个数据示例,以及它是如何组合的。如果我正确理解您需要做什么(即将几个文件附加到一个“长”文件中),那么在没有 VBA 的情况下有几种方法可以做到这一点。
-
所以该文件非常基本,只是在 A 列中的标题为“Reg. Number”,在此之下您将获得 20 到 0 个最多 9 个字符长的数字条目
-
@Jeeped 我一直在寻找类似我的问题的东西,但我只能在 Dos、python 和 R 中找到人们这样做的例子。