Sheet will not Save Properly

tclark21

New Member
Joined
Sep 14, 2009
Messages
1
Hello Everyone,
I've been searching through the forums and have not found anything that will answer my question. I think the answer is probably fairly simply, but I have not been able to determine it on my own. I have not written in VB in years and this is the first time I have ever written in VBA so please excuse some of my coding.
I am creating a user form that will do several things:
1). The user will be able to select a worksheet from a combo box, make it active, and jump to that sheet by way of a command button.
2). The user will be able to automatically format the active worksheet (with preset formatting conditions) by way of a command button (This button is not yet coded).
3). The user will be able to delete a worksheet by selecting it in the combo box and pressing a command button.
4). The user will be able add a new sheet to the workbook by naming the sheet in a text box hitting a command button. The click action will add the sheet to the combo box as well.
5). And lastly, the user will be able to Exit the macro and save the workbook by pressing the Exit command button.

The is issue that I am having revolves around the "Add New Sheet" command button. I have coded the button to read the text in the text box and name the sheet accordingly. However, I am having trouble handling an error that comes up if the new sheet has the same name as the old sheet. I was hoping someone could help me see what I'm doing wrong. It would be greatly appreciated.

Code:
If txtBox.Text = "" Or txtBox.Text = cmbSheet Then
       'User didnt input a name for the worksheet
    MsgBox "Please name your worksheet correctly!", vbCritical, "Input Box Error"
    
    'ElseIf txtBox.Text = cmbSheet.Text Then
    'User tried to assign a sheet already named that
    'MsgBox "You have tried to assign a name that is already in use. Please input correct name.", vbCritical, "Input Box Error"
    
    ElseIf txtBox.Text <> "" And txtBox.Text <> cmbSheet Then
        'Add Named sheet to combobox and workbook. Reset textbox.
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = txtBox.Text
        Me.cmbSheet.AddItem txtBox.Text
        txtBox.Text = ""
End If
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,216,143
Messages
6,129,110
Members
449,486
Latest member
malcolmlyle

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