Solved

VBA codes to convert excel file to csv file

Posted on 2015-02-08
18
374 Views
Last Modified: 2016-02-11
Hi,

What VB codes I can use to convert an excel file to a csv file in access 2010?

Thanks,
0
Comment
Question by:rowfei
  • 6
  • 4
  • 3
  • +4
18 Comments
 
LVL 3

Expert Comment

by:Shane Kahkola
ID: 40597655
Import the Excel file into a temporary table in Access, query it into a DataSet, then write it to CSV.
0
 

Author Comment

by:rowfei
ID: 40597671
Thanks. But I am trying to click a button on the form, then the codes will covert the excel file to a CSV file at the same file location.  Is this possible?
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 40597680
Try
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ws.SaveAs "C:\docs\" & ws.Name & ".csv", xlCSV
Next

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rowfei
ID: 40597686
Thanks, but I don't see the path of the excel file?
0
 
LVL 22

Expert Comment

by:Flyster
ID: 40597687
This will save your file in the same location with the same file name as a .csv file:

Sub SaveAsCSV()
Dim strCPath, strFName As String

strCPath = Application.ActiveWorkbook.Path
strFName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".csv"
  ActiveWorkbook.SaveAs Filename:= _
    strCPath & "\" & strFName, FileFormat:= _
    xlCSV, CreateBackup:=False
End Sub

Open in new window

Flyster
0
 

Author Comment

by:rowfei
ID: 40597753
Thanks.

So strCPath is the file location, the ActiveWorkbook name is the excel name?
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40597884
You do it this way in Access.

First, go to Tools, References and establish a reference to Microsoft Excel 15.0 Object Library
Then, create this function in a module:
Public Function ConvertExcel()

    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
    
    Set xls = Excel.Application
    Set wkb = xls.Workbooks.Open("c:\test\yourexcelfile.xlsx")
    Set wks = wkb.Worksheets(1)
    wks.SaveAs "c:\test\yourexcelfile.csv", xlCSV
    
    Set wks = Nothing
    wkb.Close False
    Set wks = Nothing
    xls.Quit
    Set xls = Nothing

End Function

Open in new window

Of course, adjust path and filename (and perhaps worksheet index) to those of yours.

/gustav
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40598128
Test this

Sub saveAsCSV()
Dim objExcel As Object, xlFile As String, csvFile As String
xlFile = "c:\MyFile.xls"       'change to correct location of the excel file
csvFile = Left(xlFile, Instrrev(xlFile,".")) & "csv"
Set objExcel = CreateObject("excel.Application")
objExcel.workbooks.Open (xlFile)
objExcel.activeworkbook.SaveAs csvFile, FileFormat:=6, CreateBackup:=False
objExcel.activeworkbook.Saved = True
objExcel.Quit
Set objExcel = Nothing

End Sub
0
 

Author Comment

by:rowfei
ID: 40599103
Thanks. I created a module call: "Module2". Then create a button on the form and tried both codes: "Call ConvertExcel" or "Call Module 2." I am keeping get the message:
"Compie error: Expected End Sub." But I already have "End Sub" under  "Call ConvertExcel" or "Call Module 2." Do you know the issue?


Public Function ConvertExcel()

    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
   
    Set xls = Excel.Application
    Set wkb = xls.Workbooks.Open("c:\test\yourexcelfile.xlsx")
    Set wks = wkb.Worksheets(1)
    wks.SaveAs "c:\test\yourexcelfile.csv", xlCSV
   
    Set wks = Nothing
    wkb.Close False
    Set wks = Nothing
    xls.Quit
    Set xls = Nothing

End Function
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40599156
Copy my code exactly as written.

/gustav
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40599391
Your OnClick event should read like:

Private Sub MyButton_Click()
    Call ConvertExcel
End Sub

/gustav
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40599473
you can try the codes  I posted (place it in a regular module ) and use like this

private sub yourBtn_click()
saveAsCSV

end sub
0
 

Author Comment

by:rowfei
ID: 40599540
Thank you, Rey. It works.

What codes should I add if I need to delete the excel file after it convert to CSV file?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40599578
to delete the excel file

kill xlFile
0
 

Author Comment

by:rowfei
ID: 40599593
Thanks. Just one last question, as I need to run this script daily. I am getting the message " A file named test.csv already exists in this location. Do you want to replace it" with "yes" "No, and "Cancel".

How can I pass this message to make if default replace it?

thanks,
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40599631
try this revised code

Sub saveAsCSV()
 Dim objExcel As Object, xlFile As String, csvFile As String
 xlFile = "c:\MyFile.xls"       'change to correct location of the excel file
 csvFile = Left(xlFile, Instrrev(xlFile,".")) & "csv"
 
 if dir(csvFile) <> "" then kill csvFile
 
 Set objExcel = CreateObject("excel.Application")
 objExcel.workbooks.Open (xlFile)
 objExcel.activeworkbook.SaveAs csvFile, FileFormat:=6, CreateBackup:=False
 objExcel.activeworkbook.Saved = True
 
 kill xlFile
 
 objExcel.Quit
 Set objExcel = Nothing
 
 End Sub
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40662811
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question