Solved

VBA codes to convert excel file to csv file

Posted on 2015-02-08
18
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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 51

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 51

Expert Comment

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

/gustav
0
 
LVL 51

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 48

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

630 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