Combine multiple csv to single xslx using Access VBA

佐手、 提交于 2019-12-13 00:47:25

问题


I'm having trouble trying to combine different csv files into one single xslx workbook file as separate worksheets in MS Access. Alot of the things that I've looked up were for excel, but I need to be able to use this in Access instead. Code that I find for excel vba that I try to use in a form usually gives me errors, most of the time being "Method of object '_Global' failed", even when i reference the excel library, as well as add the Excel. prefix to all of the necessary objects such as workbook and worksheet.

Edit:

This is a sample code that I tried to utilise that does a converstion from csv to xslx

 Dim CSVfolder As String
 Dim XlsFolder As String
 Dim fname As String
 Dim wBook As Excel.Workbook

 CSVfolder = "C:\test\"
 XlsFolder = "C:\test\"

 fname = Dir(CSVfolder & "*.csv")

 Do While fname <> ""
 Set wBook = Excel.Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",")
 wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), FileFormatNum = 51
 wBook.Close False
 fname = Dir
 Loop

Specifically the error gets caught at the SaveAs line, this time saying Method 'SaveAs' of object '_Workbook' failed.


回答1:


I guess you want to merge multiple csv files into one master Excel file using Access vba? if so first make a plan in your head how to achieve this.

pseudo would be:

  1. search for csv files in a folder
  2. open the csv file and copy the sheet/content to the master file
  3. save the master file
  4. Do above steps until no more files left to do

in code would be:

Private Sub Merge()
    'Create Excel application instance
    Dim xlApp As Object
    Set xlApp = CreateObject("Excel.Application")

    'Setup workbooks
    Dim wB As Excel.Workbook
    Dim wBM As Excel.Workbook

    'Csv files folder
    Dim CSVfolder As String
    CSVfolder = "C:\CsvFolder"

    'Master Excel file path
    Dim mF As String
    mF = Application.CurrentProject.path & "\Master.xlsx" 'Where your master file is

    'open the master file
    Set wBM = xlApp.Workbooks.Open(mF)

    'search and open the client files
    Dim fname As String
    fname = Dir(CSVfolder & "\*.csv")
    Do While fname <> ""
       'open the client file
       Set wB = xlApp.Workbooks.Open(CSVfolder & "\" & fname)
       'copy the first sheet from client file to master file
       wB.Sheets(1).Copy After:=wBM.Sheets(wBM.Sheets.count)
       'save master file
       wBM.Save
       'close client file
       wB.Close False
       'move to next client file
       fname = Dir()
    Loop

    xlApp.visible = True
    Set xlApp = Nothing
End Sub

Please consider trapping errors in your final code.. hope this helps.




回答2:


If you want it in Access, it's easy. Access has what's called Linked Tables. These are tables that aren't in Access, but remain whatever/wherever they already existed. Access has a link to it and you see what looks and acts like an Access table.
The command to do this is:

DoCmd.TransferText acLinkDelim, , "tblTemp", "MyFile.csv", True

You'll also want to create a query (we'll call it qryMaster) that looks like this

INSERT INTO tblMaster SELECT * FROM tblTemp

Here's the code

Sub ImportCSV()
    Dim fname As String
    Dim CSVfolder As String
    CSVfolder = "C:\test\"
    fname = Dir(CSVfolder & "*.csv")

    Do While fname <> ""
        DoCmd.TransferText acLinkDelim, , "temp", fname, True
        CurrentDb.Execute ("qryMaster")
    Loop
    DoCmd.DeleteObject acTable, "tblTemp"
End Sub

Before you run this, you'll have to create the tblmaster. The easiest way is to manually import 1 file.




回答3:


You're replacing the file extension with nothing when you should be replacing it with the correct file extension. I didn't look it up, but I'm assuming that 51 is an *.xlsx file.

 wBook.SaveAs XlsFolder & Replace(fname, ".csv", ".xlsx"), FileFormatNum = 51

By the way, you should really use the named constant instead of the magic number.



来源:https://stackoverflow.com/questions/25534640/combine-multiple-csv-to-single-xslx-using-access-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!