Feb 24, 2014
When creating macros in Excel (or another Office application), you often want to have the macro save the file of results as part of the macro. There are two main issues with saving files: where to save it (which isn't a problem with a macro because you can always write code to make sure the file gets saved in the right place), but more importantly, what to call it. If we start recording, perform the Save As action, and then stop recording, we'll have some code that looks like this:
The first line, ChDir "C:\Temp", changes the current folder that the Open/Save As menus start in when you open those menus. The second line, which is the actual Save As command, records the whole file path anyway. So, we could actually delete the first line of code, and as you can see, getting the right place isn't a problem. The problem occurs when we run the macro:
Since the file already exists, the macro has to pause to ask the user whether or not they want to overwrite it. There are two problems with this.
Problem #1:
I really don’t want the user to have to interact with the macro as it slows the whole process down. Even worse, if the user clicks on 'Cancel' then the macro stops halfway through with an error message. A workaround would be to use a VBA command that suppresses error messages such as:- Application.DisplayAlerts = False
- Or more specifically: Application.AlertBeforeOverwriting = False
- Application.DisplayAlerts = True, or
- Application.AlertBeforeOverwriting = True
Problem #2:
Do we really want a macro that overwrites (destroys) a file automatically? That sounds (and is) dangerous! When making a macro, I like to be able to say at the end, my macro CAN’T do any damage. It can fail, or not do the thing it’s meant to, but it can’t do damage to existing files. I want this macro to ALWAYS create a new file and never overwrite an existing one. So we need to be able to save the file with a unique name, a name that’s different for each time the macro gets run.I could ask the user to give me a file name, but that defeats the purpose of using the macro in the first place. A macro is meant to automate a process. A much better idea is to use a code or sequence that is never the same twice, such as the current time and date.
In Excel, there is a function called =TODAY(), which gives the current date, and =NOW(), which gives the current date and the current time. Bingo - NOW sounds like what we want.
If you put this function into the immediate window in the VBA editor, it should result to something like this:
Using the 'NOW' function by itself won't work as file names don’t like slashes or colons. Hence, we need a way to write the code so that it gets rid of those bad characters. There’s another function in Excel we could use called =TEXT. 'TEXT' has two parts inside the brackets,
- A value - which is the date and time for us, and
- A text_format - which is the format we want to see the value in
The Final Step
Now, we need to put this back into the SaveAs macro. Note that the 'FORMAT' function results with text, so all we need to do is concatenate it on the end of the file name parameter. Note that I moved the FileFormat parameter down to the next line to make it easier to read, but make sure to leave the space and underscore at the end of the first line. This way, VBA knows the code continues on the next line. Here’s the code for you to try out:Sub SaveUnique() ' ' Save a file with a unique name ' ActiveWorkbook.SaveAs Filename:="C:\Temp\Book" & Format(Now(), "yyyymmddhhmmss") & ".xlsx", _ FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False End SubNow it's your turn. Have a go and run this code in Excel VBA and see if it'll work for you. If you're interested in learning more about macros and Excel VBA, I recommend that you take a look at New Horizons' Excel VBA courses.
How do your Excel skills stack up?
Test NowNext up:
- Get-Help – the PowerShell Way
- No wonder my sales were down; I thought AIDA was an Italian Opera!
- What Lies Beneath - Part 2
- How to sort lists with multiple levels in Excel
- A Closer Look at Excel PowerPivot and Power View
- Creating Building Blocks in Microsoft Outlook
- Leonardo, Steve and Basil
- Data Deduplication in Windows Server 2012: The Solution for Ennui!
- Make CSS3 animations stay put after they've performed
- Using custom fields to calculate GST in Microsoft Project
Previously
- The new “share” feature in SharePoint 2013
- What Lies Beneath - Part 1
- I’ve got a hang up; I can’t communicate
- PowerPivot and Analysis Services Tabular Data Models
- Time saving tips in Microsoft Outlook
- Disaster Recovery in Lync Server 2013
- Master PowerPoint design with Slide & Layout Masters
- Footprints in the sand
- WCF vs Web API. How to choose?
- The fundamentals of styles in Microsoft Word