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,
What VB codes I can use to convert an excel file to a csv file in access 2010?
Thanks,
Import the Excel file into a temporary table in Access, query it into a DataSet, then write it to CSV.
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
ASKER
Thanks, but I don't see the path of the excel file?
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
Flyster
ASKER
Thanks.
So strCPath is the file location, the ActiveWorkbook name is the excel name?
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:
/gustav
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
Of course, adjust path and filename (and perhaps worksheet index) to those of yours./gustav
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:\tes t\yourexce lfile.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
"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:\tes
Set wks = wkb.Worksheets(1)
wks.SaveAs "c:\test\yourexcelfile.csv
Set wks = Nothing
wkb.Close False
Set wks = Nothing
xls.Quit
Set xls = Nothing
End Function
Copy my code exactly as written.
/gustav
/gustav
Your OnClick event should read like:
Private Sub MyButton_Click()
Call ConvertExcel
End Sub
/gustav
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
private sub yourBtn_click()
saveAsCSV
end sub
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?
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
kill xlFile
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,
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.Applic ation")
objExcel.workbooks.Open (xlFile)
objExcel.activeworkbook.Sa veAs csvFile, FileFormat:=6, CreateBackup:=False
objExcel.activeworkbook.Sa ved = True
kill xlFile
objExcel.Quit
Set objExcel = Nothing
End Sub
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.Applic
objExcel.workbooks.Open (xlFile)
objExcel.activeworkbook.Sa
objExcel.activeworkbook.Sa
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.