2

我需要将一个月的 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
4

2 回答 2

0

好的,是的,我正在为我自己的问题提供答案....将努力为建议的方法获取程序。

这是有效的代码(仅重要部分):

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
于 2013-09-19T14:43:52.127 回答
0

have you condidered using ADODB and the ODBC Text File Driver / Jet 4.0 to retrieve your data into recordsets, then dump them into worksheets:

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

so put your csv files in the path defined the variable sDataPath, set the date variable sDate (maybe within a loop) and start testing!

for more info on this type pf technique, here is the original MSDN article by Scripting Clinic (in the good old days):

MSDN: Much ADO about Text Files

Plus you'll fine a veritable plethora of information on the net using Google:)

于 2013-09-10T08:50:19.653 回答