Hello Excellers and welcome back to another #MacroMonday #ExcelVBA blog post in my 2020 series. Have you ever needed to convert a whole folder of Excel files into CSV files? Me too!. Just last week I needed to do this. I used some great Excel VBA code which had the conversion finished in no time. So I thought it would be a great topic for our Macro Monday post.
Preparing To Write The Code.
First of all, I want to make sure I have an Input Folder and an Output folder. I have created these on my Desktop. The Input folder contains my CSV files.
Next, you will need to open the Visual Basic Editor. There are two ways to do this.
- Either by hitting ALT +F11
Selecting the Developer Tab | Code Group | Visual Basic.
Both methods have the same result. You then have a choice, you can either create a module to store your code either in your Personal Macro Workbook or in your current workbook. What’s the difference?. If you save you code in your Personal Macro workbook it will be available for use in any of my Excel workbooks. If the macro is stored in the current workbook then use is restricted to that workbook. This code will be useful to reuse in any workbook. I will create and save this for future use in my Personal Macro Workbook.
Learn More About Your Personal Macro Workbook (PMW).
If you want to read more about your Excel PMW then check out my blog posts below.
Starting The Macro.
We need to start the macro by inserting a New Module. Do this by selecting the Personal.xlsb workbook, then Insert Module. Type Sub then the name of your macro. In this example, I have called it simply ConvertToCSV. Notice that Excel will automatically enter the end text End Sub to end the Sub Routine. We simply need to enter the rest of the code between these two lines.
Turn Off Screen Updates And Display Alerts.
This first piece of code will prevent the screen from flashing as the code is executed and help your code run faster. I have also chosen to not display and alerts to the user. These are pop-ups that are displayed to the user such as OK to Delete Sheet?. We do not need them in this macro so, I have decided to turn them off. Note– we will be turning these settings back on at the end of the macro.
Next, we need to declare a couple of variables. This simply creates a memory container in Excel for these values. InputCsvFile is declared as a variant. This allows the name of CSV files to be any type of name or it is not specifically declared. This data type automatically works out which data type is the most appropriate based on the value you assign. So, this makes sense when dealing with multiple Excel files that could have a myriad of names within the folder. We are declaring the input Folder as a String as well as the Output Folder.
The Input Folder and The Output Folder.
Next, have referred to the exact location of both the Input and Output folder. These are the folders that I created before beginning to write the VBA code.
Loop Through All CSV Files In The Input Folder.
I now have used the Do While loop. This requires Excel to do something if a condition is met or is true. It is like a logical function where there is a true or false condition. So, if the condition is TRUE it will keep executing the statement inside the loop but if the condition is FALSE straight away it will exit the Do While statement. In this routine, if the folder contains files (is not empty) then save those files as CSV files in the Output folder. This looping keeps going until the files are saved in the Output folder as CSV files.
Ending The Macro.
The routine then ends with the End Sub piece of code. This is already entered into the module for us when started the type the name of the Macro.
If you want more Excel and VBA tips then sign up to my monthly Newsletter where I share 3 Excel Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.
Likewise, if you want to see all of the blog posts in the Macro Mondays Series Click The Link Below
So, Don’t forget to SUBSCRIBE to the How To Excel At Excel Newsletter for more tips the first Wednesday of the month.