Create worksheet if one does not exist

hmaveal

New Member
Joined
Sep 22, 2010
Messages
11
I have a job stock spreadsheet that has all active jobs at a service center on it.
Once the job has been loaded on a truck it is marked "complete" and I move it to the current year "completed" worksheet.

How can I automate this to create a "2012","2013", etc. worksheet if it
does not exist ? I currently have a 2011 worksheet. The new worksheet would be named relavent to a cell that has the "CURRENT" year as a formula. So, on Jan 1,2012...if a job is loaded...the macro would see that there is not a 2012 worksheet ..create one..and continue with moving that job from the active aheet to the completed 2012 sheet.

I have to keep the 2011 worksheet, so just overwriting isnt feasible. I could make a copy of the 2011 and delete all the information but keep the formatting. The automatic named of the sheet as the current year is where I'm having a problem...if one doesn't already exist.

Hope that makes sense to someone .... lol
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Would this not be a one-time/year event?
Doesn't seem like a macro is wholistically necessary.
 
Upvote 0
Try the following. Place this code in "ThisWorkbook" in the VBAProject window:

Code:
Public Sub Workbook_Open()
If Not WorksheetExists(Year(Now)) Then
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = Year(Now)
End If
End Sub

And then place this code in a module within the same VBAProject.

Code:
Function WorksheetExists(WSName As Variant) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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