Userform textbox names worksheet, already exists and errors out

TimPslim

New Member
Joined
Apr 10, 2013
Messages
8
Okay, I have a sheet where you scan some barcodes and then press a button.
Once the button is pressed, a bunch of macros start and ultimately the end result is a pivot table on a new worksheet. Once the table is created on the new worksheet, the final step is a UserForm that pops up and tells the user to name the worksheet.
As I'm testing the workbook before it goes live, I've come to the realization that someone may try to name the new pivot table worksheet a name that is already taken. I tested it and it errors.
Basically, I'm looking for the code, or help finding the code, to add to my code to keep this from happening.

Here's my code as it sits - the textbox used to name the new sheet is named "custdate"

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "You MUST Enter a Name"
Cancel = True
End If
End Sub
Private Sub CommandButton1_Click()
If Trim(Me.custdate.Value) = "" Then
    Me.custdate.SetFocus
    MsgBox "You MUST Enter a Name"
    Exit Sub
End If
    ActiveSheet.NAME = Me.custdate.Text
    ActiveSheet.Move after:=Worksheets(Worksheets.Count)
    Me.custdate.Value = ""
    Me.custdate.SetFocus
    Unload Me
End Function
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
One way

Code:
End If
    On Error Resume Next
    ActiveSheet.Name = Me.custdate.Text
    On Error GoTo 0
    ActiveSheet.Move after:=Worksheets(Worksheets.Count)
    Me.custdate.Value = ""
    Me.custdate.SetFocus
    Unload Me
End Function
 
Upvote 0
When I try that, it gives me

Compile Error:
Unexpected End Sub

"End Function" is highlighted, and the
"Private Sub CommandButton1_Click()" is yellow in the debugger screen.

Is there a way to make it pop up an error box saying "Try Again" or something instead?
 
Upvote 0
Because the last line should be:
Rich (BB code):
End Sub
and not
Rich (BB code):
End Function
 
Upvote 0
This would be the complete code

Code:
Private Sub CommandButton1_Click()
If Trim(Me.custdate.Value) = "" Then
    Me.custdate.SetFocus
    MsgBox "You MUST Enter a Name"
    Exit Sub
End If
    On Error Resume Next
    ActiveSheet.Name = Me.custdate.Text
    On Error GoTo 0
    ActiveSheet.Move after:=Worksheets(Worksheets.Count)
    Me.custdate.Value = ""
    Me.custdate.SetFocus
    Unload Me
End Function

Alternatively you can use this function to check if a worksheet exists

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,270
Members
449,149
Latest member
mwdbActuary

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