I'm fairly new to VBA but have always been able to find my answers online and figure things out...until now.
I have a userform that has several comboboxes. One combobox "cmboCERNumber" list is always static while the other combobox "cmboPONumber" list changes depending on what is selected in the first combobox. The list in cmboPONumber is created temporarily on "Sheet1."
I can get the boxes to change correctly, that's not the problem.
When the form loads, if the user selects a CER Number in "cmboCERNumber" then a new sheet "Sheet1" is created. Some data is copied onto "Sheet1" and the list for "cmboPONumber" is also created. If the user then changes "cmboCERNumber" again, the "Sheet1" needs to be deleted and then the new list for "cmboPONumber" needs to be created.
My problem is that somewhere in the below code, the form is exiting after deleting "Sheet1." I don't want the form to exit.
I've narrowed my problem to the below code by clearing everything out of the sub except for this.
Private Sub cmboCERNumber_Change()
'If the temporary Sheet1 exists, delete it
Dim SheetExists As Boolean
Dim shtName As String
Dim wb As Workbook
Dim sht As Worksheet
Dim ws As Worksheet
shtName = "Sheet1"
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
SheetExists = Not sht Is Nothing
If SheetExists = True Then
For Each ws In Worksheets
If ws.Name = "Sheet1" Then
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
End
End If
Next
Else
MsgBox ("Sheet1 does not exist")
'Do other things
End If
End Sub
EDIT: I changed my method and found a better way to get to the end goal. However, I'm still curious why this exited my form.