Solved

VBA codes to convert excel file to csv file

Posted on 2015-02-08
18
352 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
 

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 49

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 119

Accepted Solution

by:
Rey Obrero 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 49

Expert Comment

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

/gustav
0
 
LVL 49

Expert Comment

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

Private Sub MyButton_Click()
    Call ConvertExcel
End Sub

/gustav
0
 
LVL 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 45

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now