Ok, here are some examples for you. The attached sheet does much more than you asked. I liked the idea of dynamically controlling sheets from one master sheet, so I played around with it. The attached sheet will allow you to add and/or rename a specified sheet, like you asked, but you can also have it run down the list and add and/or rename as needed. Also, the button on the Main Sheet reflects the sheet # being added/renamed.
I ran into some trouble when I was deleting and adding sheets, so I wrote some code that goes through and re-syncs the sheetnames with the codenames.
So, anyway, take what you can from the attached.
Below is the bare-basics of what you asked for. If there is a sheet already named what is given, it will cause an error. Also, it will not rename any sheets.
Sub AddAsLastWorksheet()
Dim SheetRow As Integer
Dim SheetsAddRename As String
SheetRow = Sheets("Main Sheet").Range("F1").Value
If Sheets("Main Sheet").Cells(SheetRow, 3).Value = "" Then
Sheets("Main Sheet").Activate
Exit Sub
Else
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Sheets("Main Sheet").Cells(SheetRow, 3).Value
End If
End Sub
*** Edit ****
Oh yeah, the attached workbook assumes that you have Tools->Options->Security->Macro Security->Trusted Publishers->"Trus access to Visual Basic Project" checkbox checked. If it is not checked, the re-syncing of sheet.codenames and sheet.names will cause an error when Main Sheet is activated.
Bookmarks