Can A Control Have More Than 1 Event Trigger?

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
OK, here's my situation as I continue to learn Excel VBA. Similar to a question I posted earlier this week that I thought I could adpat, but the circumstances are a bit different.

I have a userform (userform1) with a combobox (uf1cbx2_eqt). The user may select any one of the 8 values in the dropdown (loaded by means of the list property, not rowsource), or select "Other" if what they require isn't in the list. The process I'd like to accomplish when the user selects "Other" is to:

- default the uf1cbx2_eqt combo box with "000"
- format the text in the combobox
- prepare the default text to be overwritten, ie highlighted and focus set to that control

Then, when the user changes the value, code is run to execute some error checking. If the error check fails, then the control is reset as above allowing the user to re-enter until which time a valid entry is made (a 3 digit "number" between 1 and 999).

Below is the code I have been working with, but its not providing the results I need.

Code:
Private Sub uf1cbx2_eqt_exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    If Not mbEvents Then Exit Sub
    Dim lu1 As String
    Dim oeqt As String

    If uf1cbx2_eqt = "Other" Then
        Cancel = True
        With uf1cbx2_eqt
            mbEvents = False
            '.Value = ""
            .Locked = False
            .Text = "000"
            .ForeColor = RGB(220, 220, 220)
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(.Text)
            mbEvents = True
            Exit Sub
        End With
     End If
     If uf1cbx2_eqt.Value = "" Then
        MsgBox "Please enter a valid equipment number.", , "ENTRY ERROR : Null value"
        Cancel = True
        With uf1cbx2_eqt
            mbEvents = False
            '.Value = ""
            .Locked = False
            .Text = "000"
            .ForeColor = RGB(220, 220, 220)
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(.Text)
            mbEvents = True
            Exit Sub
        End With
    End If
    If IsNumeric(uf1cbx2_eqt) = False Then
        MsgBox "Please enter a valid equipment number.", , "ENTRY ERROR : No Text"
        Cancel = True
        With uf1cbx2_eqt
            mbEvents = False
            '.Value = ""
            .Locked = False
            .Text = "000"
            .ForeColor = RGB(220, 220, 220)
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(.Text)
            mbEvents = True
            Exit Sub
        End With
    End If
        
    neqtn = CInt(uf1cbx2_eqt.Value)
    
    If neqtn > 999 Then
        MsgBox "Please enter a valid equipment number.", , "ENTRY ERROR : Invalid number"
        Cancel = True
        With uf1cbx2_eqt
            mbEvents = False
            '.Value = ""
            .Locked = False
            .Text = "000"
            .ForeColor = RGB(220, 220, 220)
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(.Text)
            mbEvents = True
            Exit Sub
        End With
    End If
    If neqtn < 1 Then
        MsgBox "Please enter a valid equipment number.", , "ENTRY ERROR : Invalid number"
        With uf1cbx2_eqt
            mbEvents = False
            '.Value = ""
            .Locked = False
            'Cancel = True
            .Text = "000"
            .ForeColor = RGB(220, 220, 220)
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(.Text)
            mbEvents = True
            Exit Sub
        End With
    End If
         
    With ws_salt
        If WorksheetFunction.CountIf(.Rows(2), neqtn) = 0 Then 'new eqt
            .Rows(2).Find("516").Offset(, 1).EntireColumn.Insert 'insert column after RP
            eqc = .Rows(2).Find(what:="516", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False).Column
            .Range("B" & eqc) = neqtn
            MsgBox neqtn & " added to SAND OPS"
        End If
    End With
        
    With ws_sand
        If WorksheetFunction.CountIf(.Rows(2), neqtn) = 0 Then 'new staff
            .Rows(2).Find("516").Offset(, 1).EntireColumn.Insert 'insert column after RP
            eqc = .Rows(2).Find(what:="516", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False).Column
            .Range("B" & eqc) = neqtn
            MsgBox neqtn & " added to SAND OPS"
            lu1 = "P ST SD B"
        End If
    End With

    lu1 = Application.WorksheetFunction.VLookup(uf1cbx2_eqt.Value, ws_sheet2.Range("O24:P31"), 2, False)
    
....
    
    opscount = opscount + 1
    If opscount = 4 Then
        uf1cbtn1_submit.Enabled = True
        MultiPage1.Enabled = False
    End If
    mbEvents = True
    
End Sub

For the most part, the behaviour is functional, however, the user has to click out of the combobox before the event is triggered (on exit). Ideally, what works better is a change_event. But if I use a change event, the code triggers on the immediate entry of the 1st keystroke when the user tries to enter a 3 digit number. This is a problem.

Is there a way I can have both worlds? Trigger a code initially upon selection of "Other" from the combobox dropdown, and then run error checking code after the user has entered their value (on exit)?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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