Highlight text in combobox when error occurs

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
Hi all, i have a combobox that has a rowsource to a named range.

After the user enters a value, or selects a value from the list, i want to check whether the entered value is within the list of values. If not in the list, i want to warn the user.

So far i have done up till the warning part, where i prompt a messagebox with vbYesNo buttons to ask if the user wants to continue or not.

If user presses no, i want to highlight the text in the combobox.

This is the code for the combobox in the exit event
Private Sub cbx_ANRDealType_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim response As Boolean
response = ComboBoxValidation(cbx_ANRDealType)
If response = False Then
With cbx_ANRDealType
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End Sub

This is the code in a module to validate the selected/entered value
Function ComboBoxValidation(ByRef ctl As Control) As Boolean
Dim response As Variant
If ctl.ListCount <> 0 And ctl.ListIndex = -1 Then ' If an item is not selected then
response = MsgBox("The data you have entered is not in the current list. Do you still want to proceed?", vbExclamation + vbYesNo, "Data not in list")
If response = vbYes Then
ComboBoxValidation = True
ElseIf response = vbNo Then
ComboBoxValidation = False
End If
End If
End Function

This doesn't seems to work. When i test this, the text doesn't gets highlighted. Maybe it's because i press the TAB key to get out of the combobox, that's why the text doesn't gets highlighted. I don't know. Any ideas?

Thanks in advance
Shie Boon

excel 2003
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

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