ComboBox problem, a little tricky!!!

blk

Board Regular
Joined
May 27, 2003
Messages
61
Hi,

Below is some code that I'm using in a userform.

Country is the combobox name. If the user types in a country name incorrectly, it removes the last incorrect letter. The problem is, because the code runs when the value of the combobox is changed, it keeps looping and all the letters in the textbox end up being removed.

Here's the code:


Private Sub Country_Change()

If Country.MatchFound = True Then Exit Sub
Country.Text = Left(Country.Text, Len(Country.Text) - 1)
Country.SelStart = Len(Country.Text)

End Sub



Is there a way to force the code to stop looping?


Thanks


Ben
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You can use a Static Boolean variable to prevent the loop:

Code:
Private Sub Country_Change() 
   Static Abort As Boolean
   If Country.MatchFound = True Then Exit Sub 
   If Abort Then Exit Sub
   Abort = True
   Country.Text = Left(Country.Text, Len(Country.Text) - 1) 
   Country.SelStart = Len(Country.Text) 
   Abort = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,138
Members
449,361
Latest member
VBquery757

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