Compare New Sheet Name to current sheets

edwilke86

New Member
Joined
Apr 19, 2019
Messages
3
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.

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]
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Second bit of code should look like

Code:
[COLOR=#333333]if NewName = AnyCurrentSheet Name then[/COLOR]
[COLOR=#333333]MsgBox("Error: Name Exists")[/COLOR]
[COLOR=#333333]// then break[/COLOR]

[COLOR=#333333]else[/COLOR]
[COLOR=#333333]Worksheets("Template").Copy Before:=Worksheets("Template")[/COLOR]
[COLOR=#333333]Worksheets("Template (2)").Name = NewName[/COLOR]

[COLOR=#333333]end if[/COLOR]
 
Upvote 0
Hi & welcome to the board
Try
Code:
Private Sub AddNewScout_Click()

   Dim NewName As String
   NewName = InputBox("Name of Scout: (Last, First)")
   If Evaluate("isref('" & NewName & "'!A1)") Then
      MsgBox "That name already exists"
      Exit Sub
   End If

   Worksheets("Template").Copy Before:=Worksheets("Template")
   Worksheets("Template (2)").Name = NewName


End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
I read your code and it does work.
But I do not understand how this line of code can see if this sheet name already exist.
Would you please explain
If Evaluate("isref('" & NewName & "'!A1)") Then





You're welcome & thanks for the feedback
 
Last edited:
Upvote 0
ISREF is a worksheet function that checks if a value is a valid reference & returns either TRUE or FALSE.
so if you enter =ISREF(Sheet1!A1) into a cell it will return TRUE if there is a sheet called Sheet1, or FALSE if not. (The A1 can be any valid cell reference)
The Evaluate function calculates a worksheet formula in VBA, so these two lines do the same thing
Code:
Sub chk()
Debug.Print WorksheetFunction.Sum(Range("A1:A5"))
Debug.Print Evaluate("sum(A1:A5)")
End Sub

For some reason you cannot use WorksheetFunction.isref
 
Last edited:
Upvote 0
Thanks for that answer I entered nothing in A1 and the script determined somehow that the sheet already existed. Glad to see it works even if I dont understand. You explained it well but my brain cannot evaluate it.

Also never knew about:
Worksheets("Template
(2)")​
.Name = NewName

I always used Activesheet.name=NewName

I learned 2 new tricks today.




ISREF is a worksheet function that checks if a value is a valid reference & returns either TRUE or FALSE.
so if you enter =ISREF(Sheet1!A1) into a cell it will return TRUE if there is a sheet called Sheet1, or FALSE if not. (The A1 can be any valid cell reference)
The Evaluate function calculates a worksheet formula in VBA, so these two lines do the same thing
Code:
Sub chk()
Debug.Print WorksheetFunction.Sum(Range("A1:A5"))
Debug.Print Evaluate("sum(A1:A5)")
End Sub

For some reason you cannot use WorksheetFunction.isref
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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