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?
 
@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.
That would depend on what colours or groups of colours you wanted to use. There would need to be a list or some logical process to decide on the colour to use when a new day started.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
That would depend on what colours or groups of colours you wanted to use. There would need to be a list or some logical process to decide on the colour to use when a new day started.

Thanks for reply.
If is possible, in sheet2 are, starting A2, date in order for that current month (or year?) and in column B will put color we need, (or generated by code).
It will be great if possible.

Thank you.
 
Last edited:
Upvote 0
T.. in sheet2 are, starting A2, date in order for that current month ... and in column B will put color we need..
Assuming you mean that column B contains an appropriate ColorIndex number for that day, you could just modify the last line of my sub as follows.
Code:
Sheets(Sheets.Count).Tab.ColorIndex = Sheets("Sheet2").Range("B2:B32").Cells(Day(Date)).Value
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,228
Members
453,025
Latest member
Hannah_Pham93

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