如何通过VBA将一个月份的csv文件(指定date)导入到Excel中?

我需要将一个月的CSV文件加载到Excel中,以便通过VBA进行分析。 每月的每一天都是一个带有date名称(YYYYMMDD)的单独文件。

目前,我可以加载由两种不同情况创build的两个文件,A和B使用

With ActiveSheet.QueryTables.Add(Connection:=Full_F_Name_A, _ Destination:=Range("$H$4")) 

我用一个循环来改变A和B(和目的地)。 我还没有想出如何增加date。 我使用input框来获取月份中第一个文件的date。

 F_Name = InputBox("Enter name of first data file eg YYYYMMDD, target=H4, EG4") 

任何帮助将是伟大的,因为我卡住了…和初学者。

好吧,看下面的VBA代码。 收到运行时错误'3001'参数的types错误,超出可接受范围或相互冲突。 debugging器指向“.cursorlocation = aduseclient”行。 也许在我的电脑上有一些软件丢失。 ADO网站上的介绍video不再存在,所以我没有看到介绍。 我会尝试另一种方式,只要打开文件并将其转储到excel中,而我等待进一步的build议。

 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 

你有没有condidered使用ADODBODBC文本文件驱动程序/ Jet 4.0来检索您的数据到logging集,然后将其转储到工作表:

 dim cN as new adodb.connection dim rS as new adodb.recordset dim sDate as string dim sDataPath as string sDataPath="C:\Data" sdate=date ' maybe loop through date arrary, or list of dates in sheet? with cN .CursorLocation = 3 ' adUseClient .CursorType = 3 ' adopenstatic .LockType = 1 ' 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 range("A1").copyfromrecordset rs 

所以把你的csv文件定义在variablessDataPath的path,设置datevariablessDate (也许在一个循环内),并开始testing!

欲了解更多关于此types的信息,请参阅脚本诊所的原始MSDN文章(在过去的美好时光):

MSDN:很多关于文本文件的ADO

再加上你会罚款网上使用谷歌真正的信息过多

好的,我正在提供一个答案,我自己的问题….将工作获得build议的方法程序。

以下是正在工作的代码(仅限重要位):

 Dim all kinds of stuff ' Get the year and month of interest [User inputs the year and month they want the data analyzed YYYYMM YY_MM = InputBox("Enter year and month of the daily cycles you want to analyze eg YYYYMM, no day, A_, B_ or _TC needed", target = D4, AI4) 'separate the YYYYMM date to YYYY and MM F_year = Left(YY_MM, Len(YY_MM) - 2) F_month = Right(YY_MM, Len(YY_MM) - 4) 'Determine # of days in the month 'The code below is from http://msdn.microsoft.com/en-us/library/aa227538(v=vs.60).aspx and it saved me from an If than nest from hell mMax = DateSerial(CInt(F_year), (CInt(F_month) + 1), 1) - DateSerial(CInt(F_year), CInt(F_month), 1) 'The user must say where their data is by listing the path on the worksheet. in this case it is a set template sDataPath = Worksheets("Data").Cells(1, 4).Value ' value located in 1st sheet of workbook For i = 1 To mMax 'OK this is the file insertion For the A set of data files If i < 10 Then ' Need to add a zero to get the correct file name Z_singdig = "0" + CStr(i) Else Z_singdig = CStr(i) End If sDate = "A_" + YY_MM + Z_singdig + ".csv" ' looping through list of dates .. 'Label the data column with several file names so one can read it Label_F_Name = sDate + "........................." F_name = sDataPath + sDate With ActiveSheet.QueryTables.Add(Connection:="TEXT;" + F_name, Destination:=Range("D1048576").End(xlUp).Offset(1, 0)) 'insert the next file at the bottom of the first .Name = Label_F_Name .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertEntireRows .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileCommaDelimiter = True .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With 'report out the number of rows in each data file so I can use those as cell ref's later numofrows = Application.CountA(Range("D:D")) rownumout = numofrows - Corrtrow ' num of rows just added by the last file rowprtinc = 30 + i Numrowprt = "'data'!" + "DE" + CStr(rowprtinc) Range(Numrowprt) = rownumout ' entring the number of rows into a table on sheet 2 Corrtrow = numofrows ' must track the current row num so the next row num can be corrected ...yeah that worked Next i ' do the same for the B set of data files..not shown here End Sub