GingerStepChild
New Member
- Joined
- Oct 27, 2016
- Messages
- 28
I have a wee macro for creating a new sheet based on the value of a textbox.
I want to test that the textbox value does not contain any of the prohibited characters for sheetnames, namely : \ / ? * [ ].
I'm guessing that I could probably use a big NOT LIKE statement but is there a neater way to check for these characters.
I have tried writing an error handler for it but it creates a sheet then jumps to the handler when trying to name it rather than going to the handler before trying to name the sheet.
Some code
Thanks in advance.
I want to test that the textbox value does not contain any of the prohibited characters for sheetnames, namely : \ / ? * [ ].
I'm guessing that I could probably use a big NOT LIKE statement but is there a neater way to check for these characters.
I have tried writing an error handler for it but it creates a sheet then jumps to the handler when trying to name it rather than going to the handler before trying to name the sheet.
Some code
Code:
Sub AddPlayer()
'
' This sub will copy the games sheet into a new worksheet and call it after the new player.
' Each player must be unique so it will check if the name has already been taken
Dim ws1 As Worksheet
On Error Resume Next
Set ws1 = Sheets(OpenUserForm.TB_AddPlayer.Value)
On Error GoTo 0
If Not ws1 Is Nothing Then
MsgBox "Already existing, choose another name"
Else
On Error GoTo invalidchar:
If OpenUserForm.TB_AddPlayer.Value > "" Then
Worksheets("NewPlayerSheet").Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
ActiveSheet.Name = OpenUserForm.TB_AddPlayer.Value
MsgBox "Player called " & OpenUserForm.TB_AddPlayer.Value & " has been added"
MakePlayerList ' create an A-Z ordered player list in the drop down box.
Else
RestartHere:
End If
End If
Exit Sub
invalidchar: 'Error handling for an invalid sheetname.
' message box then change the text box value to 'add player'
MsgBox "Cannot use that name"
OpenUserForm.TB_AddPlayer.Value = "Add Player"
Resume RestartHere:
End Sub