DenverWill81
New Member
- Joined
- Apr 29, 2022
- Messages
- 7
- Office Version
- 2016
Long time reader, first time poster!
I'm trying to create a Loop that names a sheet based on a cell value. If the sheet name already exists then the macro will pop up a message box with an entry field to enter a new name and that will Loop until the user enter a sheet name that isn't already taken. Here's what I've got...
I'm trying to create a Loop that names a sheet based on a cell value. If the sheet name already exists then the macro will pop up a message box with an entry field to enter a new name and that will Loop until the user enter a sheet name that isn't already taken. Here's what I've got...
VBA Code:
Sub NameSheet()
'
' The idea is to copy the HDS Import Template and make an attempt to rename it based on the value of "IMPORT_ProjectName". If there's already a sheet with that name _
' then you get a Message Box where you can enter a new name. The macro then attempts to use the new name but if it too exists then it loops back up to the Message Box _
' until you enter a name that doesn't yet exist. I just can't quite get the Do Until / Loop function figured out.
' IMPORT HDS
' Set Project Name
Dim PN As range
Set PN = range("IMPORT_ProjectName")
' Copy HDSImport Template and Rename
Sheets("HDS Import Template").Copy After:=Sheets(Sheets.Count)
On Error GoTo PNmsgbox
ActiveSheet.Name = PN
GoTo HDSSuccess
PNmsgbox:
' Do until... No Error???
Dim strInput As String
strInput = InputBox(range("IMPORT_ProjectName").Value & " already exists." & vbNewLine & vbNewLine & "Please enter a new name in the box below or click Cancel", "Project Name", "")
range("IMPORT_ProjectName").Value = strInput
On Error GoTo NextLoop
ActiveSheet.Name = PN
GoTo HDSSuccess
NextLoop:
' Loop...
HDSSuccess:
End Sub