Worksheet Creation Help Needed

DaveUK

Board Regular
Joined
Jan 24, 2005
Messages
245
Please could someone suggest VBA code to do the following:

I have a Workbook Called "SALES".

I want to create a yearly worksheet but obviously only if it doesn't exist.

So if for example it was January 2006 then i want to check in the active workbook, called "SALES", for the existance of a worksheet called "2006". If it doesn't exist i want to create one named "2006", if it does exist then i don't need to create it.

Also how do i then after making the worksheet, how do i then call it to apply some VBA. I need to check for the year and then set this to a string called "CurrentYear". Bear in mind that a worksheet "2005" exists so to access this worksheet i would hope that "CurrentYear" will equal "2005". Whereas if it is January 2006 i hope that "CurrentYear" will equal "2006" and so on and so forth.

I assume it will be similar to this :

With Workbook("SALES).Worksheets(CurrentYear).

' Code here

End with

But i am not sure !!!

Please advise.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Have a go with this code and see if you can adapt it. The Test procedure determines the current year and calls another procedure to add a sheet if it does not exist. You can then use the sCurrentYear variable to refer to the current year's sheet.

Code:
Sub Test()
Dim sCurrentYear As String

sCurrentYear = CStr(Year(Now))
AddSheetIfDoesNotExist (sCurrentYear)

ThisWorkbook.Worksheets(sCurrentYear).Activate
End Sub

Sub AddSheetIfDoesNotExist(sSheetName As String)
Dim bolSheetExists As Boolean

bolSheetExists = False
With ThisWorkbook
    For Each sht In .Worksheets
        If sht.Name = sSheetName Then
            bolSheetExists = True
            Exit For
        End If
    Next sht
    
    If Not bolSheetExists Then .Worksheets.Add(After:=.Worksheets(.Worksheets.Count)).Name = sSheetName
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,255
Messages
6,077,313
Members
446,278
Latest member
hoangquan2310

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