Excel VBA - Guide To Creating A New Sheet Everyday Automatically
When excel is opened, VBA can start immediately helping you to do tasks automatically. Once of the most useful I have found is to create a new sheet everyday for me to work from.
This simple, yet effective bit of code will create a new sheet, name the sheet as Todays date and place it at the end of the sheet tab. There's also a Bonus section at the end for generating a new sheet based on a template, follow the normal steps first and then read the Bonus section.
Step 1 - Accessing VBA
VBA can be accessed in one of two ways:
- Simply press ALT + F11 when Excel is open on a new workbook
- Head to the Options and select "View Developer Tab" then click Visual Basic (2007 Onwards)
When the editor opens, you'll be presented with a grey window with a project manager down the left hand side.
Double click on "ThisWorkbook" - This is where we'll be writing our code for this guide. When double clicked, you'll be presented with a blank document on the right hand side of the window
Step 2 - Write VBA For Detecting The Workbook Opening
In the blank space on the right hand side, write the following code:
Private Sub Workbook_Open() End Sub
This now tells Excel that when the workbook is opened, everything between these lines needs to be executed.
Step 3 - Adding A New Sheet
The code for adding a new sheet is really simple, as simple as "Sheets.Add". But the problem with using the bog standard, easy way is that the sheet will be added with the name "Sheet X" and will appear as the first sheet in the workbook.
We want to add a sheet, name it as today's date and place it at the end of the sheet tab at the bottom of the Excel window.
Write the following code between the two lines written in Step 1
Sheets.Add(After:=Sheets(Sheets.Count)).Name = TodaysDate
The "Sheets.Add" is still the part of the code that is adding the sheet, but we are giving it some extra parameters in order to tailor how it works to our needs. After the brackets open, the "After:=" tells VBA to expect a parameter to tell it where to place the sheet. The "Sheets(Sheets.Count)" is telling the "After" command that it should go After the sheet with the index number that is the same as the amount of sheets in the workbook, the last sheet.
The ".Name" on the end allows us to specify a name during the creation of the workbook. "TodaysDate" is a variable tag, which we haven't defined yet, we'll do that now
Step 4 - Getting Today's Date
If we're going to name the new worksheet as today's date, we need to know what the date is.
Underneath the opening line you wrote in step 1 ("Private Sub Workbook_Open()"), write the following code:
'Get Todays Date Dim TodaysDate As String TodaysDate = Format(Now(), "dd-mm-yyyy")
The "Dim TodaysDate As String" sets the variable up as an empty string
The "TodaysDate = Format(Now(), "dd-mm-yyyy")" is getting the date and time as it is right now, and converting it in to the format of Day, Month, Year. The result is then placed in the variable "TodaysDate"
This variable will then be used in the previous step to name the worksheet as the current date
Step 5 - Checking The Worksheet Doesn't Already Exist
Before we move on, let's quickly check that you're code is up to date, it should look like this at the moment:
Private Sub Workbook_Open() 'Get Todays Date Dim TodaysDate As String TodaysDate = Format(Now(), "dd-mm-yyyy") Sheets.Add(After:=Sheets(Sheets.Count)).Name = TodaysDate End Sub
We might open the workbook more than once during the same day, if so, we want to stop the workbook from trying to create another sheet for today (it will fail and throw an annoying error message).
Place the following code between your Date code and your Add Sheet code
'Check Todays Sheet Doesn't Already Exist Dim SheetExists As Boolean For sheetnames = Worksheets.Count To 1 Step -1 If Worksheets(sheetnames).Name = TodaysDate Then SheetExists = True Exit For End If Next
The "Dim SheetExists As Boolean" creates a new variable that is either true or false (false by default).
The "For sheetnames = Worksheets.Count To 1 Step -1" sets up a variable called "sheetnames" and assigns it a value of the number of sheets set by the "Worksheets.Count" command. The "To 1 Step - 1" tells the code to count backwards from "sheetnames" by 1 until all the value of "sheetnames" is equal to 1.
The code between the "For" and "Next" is then executed on every iteration through the number of sheets. If the sheet that is being checked is found to have Todays Date as it's name, the "SheetExists" variable is set to True and the checks stop. If no sheet is found then the "SheetExists" variable remains False
Now all that is left to do is to ensure that if the sheet does exist already, not to execute the command to create a new sheet.
Wrap the Add command in the following so that it looks like this:
If SheetExists = False Then Sheets.Add(After:=Sheets(Sheets.Count)).Name = TodaysDate End If
And that's it, you now have a code that runs every time the workbook is opened, that creates a new sheet, names it today's date, places it at the end of the tab bar and checks if one is already created. Simply click the save button in excel and save as a "Macro Enabled Workbook", close everything, open it up again and see it create your first auto-generated sheet.
(You can also click the Play button in the VBA editor when the cursor is between the first and last line of the code to run it instantly)
Your complete code should look like the following:
Private Sub Workbook_Open() 'Get Todays Date Dim TodaysDate As String TodaysDate = Format(Now(), "dd-mm-yyyy") 'Check Todays Sheet Doesn't Already Exist Dim SheetExists As Boolean For sheetnames = Worksheets.Count To 1 Step -1 If Worksheets(sheetnames).Name = TodaysDate Then SheetExists = True Exit For End If Next If SheetExists = False Then Sheets.Add(After:=Sheets(Sheets.Count)).Name = TodaysDate End If End Sub
Bonus - Adding A New Sheet From A Template
Just incase you want a sheet with a template generated everyday opposed to a blank sheet, simply change the last section of the code to the following:
If SheetExists = False Then Worksheets("Sheet1").Copy After:=Worksheets(Worksheets.Count) ActiveSheet.Name = TodaysDate End If
This will copy the format in "Sheet1" (Or whatever your template sheet is called) to a new sheet.
TIP: After making your Template sheet, right click it in the sheet tab at the bottom of the page and set it to "Hidden" by clicking "Hide". The VBA code will still be able to read it, and you wont have a template tab just sat there.
Note: If the template sheet is changed, the changes will only be applied to new sheets that are generated after the change.
Following the guide above, did you get this working?
How Did It Go?
If you couldn't get this working, pop a comment below explaining where you got stuck and I'll help where I can or change this hub to reflect where people are having issues