I've been through this a dozen times and can't seem to work out what's wrong!
I have a combobox (cboGVW), which represents the gross vehicle weight of a truck. The combobox is populated with standard manufacturers weights, but sometimes a custom weight needs to be entered so .matchrequired = false.
I am trying to:
a) Allow numeric or null entries only.
b) Set upper and lower limits for numeric entries.
The code below works in every instance except for allowing null entries! Once the combobox has been selected, I'm stuck there until I enter a valid number!
VB Code:
Private Sub cboGVW_Exit(ByVal Cancel As MSForms.ReturnBoolean) 'GVW ENTRY FORMAT VALIDATION If IsNumeric(cboGVW.Value) And cboGVW.Value >= 7500 And cboGVW.Value <= 32000 Then cboGVW.Value = Format(CInt(cboGVW.Value)) ElseIf IsNumeric(cboGVW.Value) And cboGVW.Value < 7500 Then MsgBox "Skiploaders are not suitable for vehicles with a GVW less than 7500kg!" & vbCr & _ "Please enter a valid GVW or consult Engineering.", vbExclamation + vbOKOnly, "Invalid Entry!" cboGVW.Value = Null Cancel = True ElseIf IsNumeric(cboGVW.Value) And cboGVW.Value > 32000 Then MsgBox "Skiploaders are not suitable for vehicles with a GVW greater than 32000kg!" & vbCr & _ "Please enter a valid GVW or consult Engineering.", vbExclamation + vbOKOnly, "Invalid Entry!" cboGVW.Value = Null Cancel = True ElseIf Not IsNull(cboGVW.Value) Then MsgBox "Entries must be numeric and between 7500 and 32000!" & vbCr & _ "Please enter a valid GVW or consult Engineering.", vbExclamation + vbOKOnly, "Invalid Number Format!" cboGVW.Value = Null Cancel = True Else End If End Sub
As usual any help to relieve my pain would be appreciated.
EDIT: Sometimes I should take a step back before I ask! Problem has been resolved, although constructive criticism is always welcome.
Modified Code:
VB Code:
Private Sub cboGVW_Exit(ByVal Cancel As MSForms.ReturnBoolean) 'GVW ENTRY FORMAT VALIDATION If IsNumeric(cboGVW.Value) And cboGVW.Value >= 7500 And cboGVW.Value <= 32000 Then cboGVW.Value = Format(CInt(cboGVW.Value)) ElseIf IsNumeric(cboGVW.Value) And cboGVW.Value < 7500 Then MsgBox "Skiploaders are not suitable for vehicles with a GVW less than 7500kg!" & vbCr & _ "Please enter a valid GVW or consult Engineering.", vbExclamation + vbOKOnly, "Invalid Entry!" cboGVW.Value = Null Cancel = True ElseIf IsNumeric(cboGVW.Value) And cboGVW.Value > 32000 Then MsgBox "Skiploaders are not suitable for vehicles with a GVW greater than 32000kg!" & vbCr & _ "Please enter a valid GVW or consult Engineering.", vbExclamation + vbOKOnly, "Invalid Entry!" cboGVW.Value = Null Cancel = True [I] [B] ElseIf IsNull(cboGVW.Value) Then 'Do nothing[/B] [B]Else MsgBox "Entries must be numeric and between 7500 and 32000!" & vbCr & _ "Please enter a valid GVW or consult Engineering.", vbExclamation + vbOKOnly, "Invalid Number Format!" cboGVW.Value = Null Cancel = True[/B][/I] End If End Sub
EDIT: It's obviously later than I thought - the above doesn't work either!!!![]()



Reply With Quote