Try these three event handlers. This example uses 'Lookup' as the sheet containing the codes and items. It assumes the Listbox is named 'ListBox1'.
Attach this code to the command button on your userform (change the Sub name to reflect the name of your command button):
Private Sub cboCreateNew_Click()
On Error GoTo errhandler
Sheets.Add
ActiveSheet.Name = Me.ListBox1.Value
Exit Sub
errhandler:
MsgBox "A worksheet already exists for " & UCase(Me.ListBox1.Value), vbCritical, "Duplicate Entry"
Application.DisplayAlerts = False
ActiveSheet.Delete
End Sub
Attach this code to the option button for 'Serving' (change the Sub name to reflect the name of your option button):
Private Sub optServing_Click()
Dim i, LastRow
LastRow = Sheets("Lookup").Range("A" & Rows. _
Count).End(xlUp).Row
Me.ListBox1.Clear
For i = 1 To LastRow
If Left(Sheets("Lookup").Cells(i, "A"), 1) = 1 Then
With Me.ListBox1
.AddItem Sheets("Lookup").Cells(i, "B").Value
End With
End If
Next
End Sub
Attach this code to the option button for 'SetUp' (change the Sub name to reflect the name of your option button):
Private Sub optSetup_Click()
Dim i, LastRow
LastRow = Sheets("Lookup").Range("A" & Rows. _
Count).End(xlUp).Row
Me.ListBox1.Clear
For i = 1 To LastRow
If Left(Sheets("Lookup").Cells(i, "A"), 1) = 2 Then
With Me.ListBox1
.AddItem Sheets("Lookup").Cells(i, "B").Value
End With
End If
Next
End Sub
Create a button on your Lookup sheet and attach this macro to it:
Sub AddNew_and_Sort()
AddCode = InputBox("Please enter the Code to add.", "New Code")
If AddCode = "" Then
Exit Sub
End If
AddItem = InputBox("Please enter the Item to add.", "New Item")
If AddItem = "" Then
Exit Sub
End If
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = AddCode
Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = AddItem
Columns("A:B").Select
Selection.Sort Key1:=Range("A1"), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
End Sub
The above macro will query for a new code and item name, add it to the table, then sort the table ascending based on the codes.