Trying to do something that should be simple, I'm just not familiar with VBA syntax. I'm decent with C++, Python, and LUA, but I don't know about VBA. I have a command button that prompts the user for a name, then creates a sheet based on a template sheet with that name. That works fine.
It has a problem, if there is already a sheet with the same name, an error is generated, and a new sheet named "Template (2)" is created. I don't want my user filling the book up with redundant templates, obviously. So what I'm trying to do is this, though obviously this isn't correct.
Code:
Private Sub AddNewScout_Click()
Dim NewName As String
NewName = InputBox("Name of Scout: (Last, First)")
Worksheets("Template").Copy Before:=Worksheets("Template")
Worksheets("Template (2)").Name = NewName
End Sub
It has a problem, if there is already a sheet with the same name, an error is generated, and a new sheet named "Template (2)" is created. I don't want my user filling the book up with redundant templates, obviously. So what I'm trying to do is this, though obviously this isn't correct.
Code:
if NewName = AnyCurrentSheet Name then
MsgBox("Error: Name Exists")
// then break
else
Worksheets("Template").Copy Before:=Worksheets("Template")
Worksheets("Template (2)").Name = NewName
end if [code]
I just need to know the syntax to compare NewName to all the current sheet names, so I can break if it matches and not create a sheet at all. My Google-Fu has failed me for hours.
Thank you!
[I]Using Office 365, also. [/I]