VBA converting .csv files to .xlsx in a new location.

BrianG86

Board Regular
Joined
Nov 12, 2013
Messages
135
Hi,

I have a folder full of files that are extracted from our in house system on a daily basis into a .csv file.

What I need to do is have a code that will convert these all to .xlsx files into a new location every morning.

I don't even know where to start with this. Can anyone help?

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this

Change your CSV file folder and excel file folder as per need

HTML:
Sub CSVtoXls()


 Dim CSVfolder As String
 Dim XlsFolder As String
 Dim fname As String
 Dim wBook As Workbook
 
 CSVfolder = "C:\csvfolder\"
 XlsFolder = "C:\xlsFolder\"
 
 fname = Dir(CSVfolder & "*.csv")
 
 Do While fname <> ""
    Set wBook = Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",")
    wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), ThisWorkbook.FileFormat
    wBook.Close False
 fname = Dir
 Loop
 
End Sub
 
Last edited:
Upvote 0
Hi,

Thanks for the reply. This is not working for me. I am new to this, so is there anything I need to amend on the above code?

I have input it as below:

Code:
Sub CSVtoXls()



 Dim CSVfolder As String
 Dim XlsFolder As String
 Dim fname As String
 Dim wBook As Workbook
 
 CSVfolder = "T:\08. CaseBlocks Data for Reports\Stephanie"
 XlsFolder = "C:\UserData"
 
 fname = Dir(CSVfolder & "*.csv")
 
 Do While fname <> ""
    Set wBook = Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",")
    wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), ThisWorkbook.FileFormat
    wBook.Close False
 fname = Dir
 Loop
 
End Sub
 
Upvote 0
if I guess it correctly this is where the problem is :

Code:
CSVfolder = "T:\08. CaseBlocks Data for Reports\Stephanie"
XlsFolder = "C:\UserData

Change is by this --- Just a "\" added at last position

Code:
CSVfolder = "T:\08. CaseBlocks Data for Reports\Stephanie\"
XlsFolder = "C:\UserData\
 
Upvote 0
Hi, also note that if you would like to convert your files to a specific format then you need to change:

wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), ThisWorkbook.FileFormatTO
wBook.SaveAs XlsFolder & Replace(fname, ".csv", ".xls")</pre></pre>
so the modified version will read:

Sub CSVtoXls() Dim CSVfolder As String Dim XlsFolder As String Dim fname As String Dim wBook As Workbook CSVfolder = "C:\csvfolder\" XlsFolder = "C:\xlsFolder\" fname = Dir(CSVfolder & "*.csv") Do While fname <> "" Set wBook = Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",") wBook.SaveAs XlsFolder & Replace(fname, ".csv", ".xls") wBook.Close False fname = Dir Loop End Sub</pre>
 
Upvote 0
It works, but the result files are still saved as .csv, and could not be opened directly in excel, any thought? Thanks. I changed the .xls to .xlsx
 
Upvote 0
Try this

Change your CSV file folder and excel file folder as per need

HTML:
Sub CSVtoXls()


 Dim CSVfolder As String
 Dim XlsFolder As String
 Dim fname As String
 Dim wBook As Workbook
 
 CSVfolder = "C:\csvfolder\"
 XlsFolder = "C:\xlsFolder\"
 
 fname = Dir(CSVfolder & "*.csv")
 
 Do While fname <> ""
    Set wBook = Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",")
    wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), ThisWorkbook.FileFormat
    wBook.Close False
 fname = Dir
 Loop
 
End Sub

===================================================

Hello MrExcel:
I'm new on this site, I found this code that i will like to use it, my question is : How I can change the 'XlsFolder' to use a Browser for a folder location?.
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,761
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top