Link to home
Start Free TrialLog in
Avatar of rowfei
rowfei

asked on

VBA codes to convert excel file to csv file

Hi,

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

Thanks,
Avatar of Eric Greene
Eric Greene
Flag of United States of America image

Import the Excel file into a temporary table in Access, query it into a DataSet, then write it to CSV.
Avatar of rowfei
rowfei

ASKER

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?
Try
Dim ws As Worksheet

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

Open in new window

Avatar of rowfei

ASKER

Thanks, but I don't see the path of the excel file?
Avatar of Flyster
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
Avatar of rowfei

ASKER

Thanks.

So strCPath is the file location, the ActiveWorkbook name is the excel name?
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
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rowfei

ASKER

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
Copy my code exactly as written.

/gustav
Your OnClick event should read like:

Private Sub MyButton_Click()
    Call ConvertExcel
End Sub

/gustav
you can try the codes  I posted (place it in a regular module ) and use like this

private sub yourBtn_click()
saveAsCSV

end sub
Avatar of rowfei

ASKER

Thank you, Rey. It works.

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

kill xlFile
Avatar of rowfei

ASKER

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,
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
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.