Error handling on a UserForm

litestream

Active Member
Joined
Jul 24, 2006
Messages
323
I've got my code up and running now, but is it possible to insert another worksheet and rename it as the value of TextBox1 if the worksheet doesn't exist? And how about if a worksheet with that name already exists?

My code is:

Private Sub UserForm_Initialize()
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1:A" & LastRow).Name = "ListName"
ComboBox1.RowSource = "ListName"

End Sub

Private Sub ComboBox1_Change()
Dim BatchLet As String

With ComboBox1
TextBox1.Text = Range(.RowSource).Cells(.ListIndex + 1, 2)
BatchLet = TextBox1.Value
TextBox2.Value = Sheets(BatchLet).Select
TextBox2.Value = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Value
End With

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You have not stated what you want to do if the sheet exists. Here just gets a message.
Code:
'=============================================================================
'- TEXTBOX VALUE FOR NEW SHEET NAME
'- CHECK SHEET NAME DOES NOT EXIST.
'=============================================================================
Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim SheetName As String
    Dim SheetExists As Boolean
    '------------------------------------------------------------------------
    SheetName = TextBox1.Value
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name = SheetName Then
                SheetExists = True
                Exit For
        End If
    Next
    '------------------------------------------------------------------------
    '- ACTION
    If SheetExists Then
        MsgBox ("A sheet named " & SheetName & " already exists" & vbCr _
            & "Please change text box name.")
    Else
        ActiveWorkbook.Sheets.Add
        ActiveSheet.Name = SheetName
    End If
    '-------------------------------------------------------------------------
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,316
Members
448,564
Latest member
ED38

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