我需要将一个月的 CSV 文件加载到 Excel 中,以便通过 VBA 进行分析。每月的每一天都是一个带有日期名称 (YYYYMMDD) 的单独文件。
目前,我可以加载由两种不同情况创建的两个文件,A 和 B 使用
With ActiveSheet.QueryTables.Add(Connection:=Full_F_Name_A, _
Destination:=Range("$H$4"))
我使用循环来更改 A 和 B(以及目的地)。我还没有弄清楚如何增加日期。我使用输入框来获取当月第一个文件的日期。
F_Name = InputBox("Enter name of first data file eg YYYYMMDD, target=H4, EG4")
任何帮助都会很棒,因为我被卡住了......而且是初学者。
OK OK,请看下面的 VBA 代码。收到的运行时错误“3001”参数类型错误、超出可接受范围或相互冲突。调试器指向“.cursorlocation = aduseclient”行。也许我的电脑上缺少一些软件。ADO 网站上的介绍视频不再存在,所以我没有看到介绍。我将尝试另一种我知道的方法,即打开文件并将它们转储到 excel 中,同时等待进一步的建议。
Sub Month_wdata_import()
Set cN = CreateObject("ADODB.Connection")
Set rS = CreateObject("ADODB.Recordset")
Dim sDate As String
Dim sDataPath As String
Dim i As Integer
Dim mMax As Integer
sDataPath = Worksheets("D&L").Cells(1, "G").Value ' values located in 2nd sheet of workbook
mMax = Worksheets("D&L").Cells(1, "D").Value 'values located in 2nd sheet of workbook
For i = 1 To mMax
sDate = "A_" + CStr(Worksheets("D&L").Cells(1 + i, "A").Value) ' looping through list of dates in sheet
With cN
.cursorlocation = aduseclient
.CursorType = adopenstatic
.LockType = adLockreadonly
.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDataPath & ";" & _
"Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;""")
End With
With rS
.ActiveConnection = cN
.Source = "select * from data_" & sDate & "_.csv"
.Open
End With
Next
Range("A1").CopyFromRecordset rS
End Sub