Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Need to loop thru folder, save only the .xlsx files as .csv

0
Sub DirectoryFileLoop2()
    'Loop through files in a folder containing only .xlsx files and .csv files.
    'Leave the .csv files as is,  but save the .xlsx files as .csv files.
    'It does not work because it trys to save the .xlsx files as .xlsx.csv files
    Dim fileDirectory As String
    Dim fileCriteria As String
    Dim fileName As String
    Dim fileToSave As String
    Dim fileToOpen As Workbook

    Application.ScreenUpdating = False

    fileDirectory = "C:\Test\Directory\"
    fileCriteria = "*.xlsx"

    fileName = Dir(fileDirectory & fileCriteria)
    fileToSave = Dir(fileDirectory)  'attempt to save fileName without *.xlsx but
    ' it does not.  fileToSave is exactly the same as fileName

    Do While Len(fileName) > 0

        Set fileToOpen = Workbooks.Open(fileDirectory & fileName)
        fileToOpen.SaveAs fileName:=fileDirectory & fileToSave & "*.csv"
        'fileToOpen.SaveAs fileName:=fileDirectory & fileToSave, FileFormat:=xlCSV
        fileToOpen.Close True

        Debug.Print fileName

        fileName = Dir

    Loop

    Application.ScreenUpdating = True


End Sub
Answer
Discuss

Answers

0
Selected Answer

Hello TDog and welcome to the forum,

Your code is close but it needs to remove the ".xlxs" file extesion from the name before saving it as a ".csv" file. I have modified your code to the following. I have tested it using Excel 2016 and works fine on my laptop. (changes are in bold)

Sub DirectoryFileLoop3()

    'Loop through files in a folder containing only .xlsx files and .csv files.
    'Leave the .csv files as is,  but save the .xlsx files as .csv files.

Dim fileDirectory As String
Dim fileCriteria As String
Dim fileName As String
'Dim fileToSave As String   ' not required
Dim fileToOpen As Workbook

Application.ScreenUpdating = False

    fileDirectory = "C:\Test\Directory\"
    fileCriteria = "*.xlsx"

    fileName = Dir(fileDirectory & fileCriteria)
    'fileToSave = Dir(fileDirectory)   ' not required

Do While Len(fileName) > 0

    Set fileToOpen = Workbooks.Open(fileDirectory & fileName)

        ' get file name without extension
        fileName = Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1)
        'MsgBox fileName

        fileToOpen.SaveAs fileName:=fileDirectory & fileName, FileFormat:=xlCSV
        fileToOpen.Close True
        'Debug.Print fileName & ".csv"

        fileName = Dir

    Loop

    Application.ScreenUpdating = True

End Sub
'

If this solves things for you please mark my answer as Selected

Cheers   :-)

Discuss

Discussion

Thank you WillieD24 for the welcome and for the quick answer!
TDog
TDog (rep: 2) Mar 9, '24 at 6:27 pm
You're welcome, glad I was able to help. Thanks to the comments in your code it made it easy to understand what you wanted to achieve.
I would like to suggest that when you begin to write vba code, start with "Option Explicit" at the top of the code window. This will cause the VB editor to point out / flag any errors. This can help you learn and many times to solve issues on your own. For issues you can't solve, post your question here and hopefully one of us can help you out.

Thanks for selecting my answer.

Cheers   :-)
WillieD24 (rep: 557) Mar 9, '24 at 9:19 pm
@Willie - good one! Quick question- have you heard from Don recently? Suspect he's too busy on commercial projects to create TeachExcel instructional videos. Or am I missing those emails too? (I don't get emails when someone responds to an Answer say and haven't for a couple of years now ) 
John_Ru (rep: 6142) Mar 10, '24 at 11:59 am
@John
No, I too haven't heard from Don recently. I miss getting those emails which include Excel or VBA tutorials. The last such email I got was in Oct/23. Also, I only get emails when a new question is posted, but not when someone comments on or selects my answer. The only way to check for that is to login to the forum. Nonetheless, I will continue to help out when I can (except when it means remaking the workbook and the code)

Cheers   :-)
WillieD24 (rep: 557) Mar 10, '24 at 4:32 pm
@Willie - thanks. I'll try to ask Don tomorrow. Have a good week. 
John_Ru (rep: 6142) Mar 10, '24 at 6:07 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login