Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA codes to convert excel file to csv file

Posted on 2015-02-08
18
Medium Priority
?
517 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
17 Comments
 
LVL 4

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 11

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 52

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 2000 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 52

Expert Comment

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

/gustav
0
 
LVL 52

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 49

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

782 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