Excel VBA to Add New Sheet then Change Name to Current Date and Tab Color

astrid22

New Member
Joined
Mar 3, 2018
Messages
10
Hi, I am relatively new to Excel VBA and I am trying to create a personal template for my work. I already have the below code to insert a new sheet. But I don't know how to change the tab color based on a fixed cell value.
Sub GenerateConf()
On Error GoTo MyError
Sheets.Add
ActiveSheet.Name = WorksheetFunction.Text(Now(), "mm.dd.yyyy" + " OA ")
Exit Sub
MyError:
MsgBox "There is already a sheet called that."
End Sub

Cell J22 contains the letters OA. I want to be able to insert a new sheet, rename it to the current date + the letters OA + the count of sheets with OA as part of its name on the same day (like (date today) OA 1; (date today) OA 2; (date today) OA 3, etc) and then change the tab color to light green.

Any ideas?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Code:
Sub GenerateConf()
 On Error GoTo MyError
 Sheets.Add
 With ActiveSheet
    .Name = WorksheetFunction.Text(Now(), "mm.dd.yyyy" + " OA ")
    .Tab.ColorIndex = 35
 End With
 Exit Sub
MyError:
 MsgBox "There is already a sheet called that."
 End Sub
 
Upvote 0
Hi, thank you! Works great! But how do I prevent it from still inserting a new sheet if the sheet name already exists?
 
Upvote 0
Cell J22 where? All we know about your workbook is from your code which is adding a brand new worksheet which won't have anything in J22.

Hi, sorry. Vbagreenhorn1 has already solved the tab color part. I only set J22 cell to have the letters OA as I was thinking about using it as a condition to change the color. Like I mentioned I am relatively new to Excel VBA so I am still trying to find my way around it :)
 
Upvote 0
Hi, thank you! Works great! But how do I prevent it from still inserting a new sheet if the sheet name already exists?
I don't understand that part. You said ..

I want to be able to insert a new sheet, rename it to the current date + the letters OA + the count of sheets with OA as part of its name on the same day (like (date today) OA 1; (date today) OA 2; (date today) OA 3, etc)
If we do the blue part, as my code below does, then there will never be an attempt to name a sheet the same as one already in the workbook. :confused:

Code:
Sub GenerateConf_v2()
  Dim ws As Worksheet
  Dim NameCount As Long
  Dim NameBase As String
  
  NameBase = Format(Date, "mm.dd.yyyy OA ")
  For Each ws In Worksheets
    If ws.Name Like NameBase & "#*" Then NameCount = NameCount + 1
  Next ws
  Sheets.Add(After:=Sheets(Sheets.Count)).Name = NameBase & NameCount + 1
  Sheets(Sheets.Count).Tab.ColorIndex = 35
End Sub
 
Upvote 0
I don't understand that part. You said ..

If we do the blue part, as my code below does, then there will never be an attempt to name a sheet the same as one already in the workbook. :confused:

Code:
Sub GenerateConf_v2()
  Dim ws As Worksheet
  Dim NameCount As Long
  Dim NameBase As String
  
  NameBase = Format(Date, "mm.dd.yyyy OA ")
  For Each ws In Worksheets
    If ws.Name Like NameBase & "#*" Then NameCount = NameCount + 1
  Next ws
  Sheets.Add(After:=Sheets(Sheets.Count)).Name = NameBase & NameCount + 1
  Sheets(Sheets.Count).Tab.ColorIndex = 35
End Sub


Hi Peter, this is exactly what I am trying to do! Thank you so much! Saved me a lot of time! :)
 
Upvote 0
@Peter_SSs,

How would your code change as every day the tab has another color?
Obviously the tabs created on the same day have the same color.
Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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