exit sub when textbox contains certain item

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hi all
I have a textbox called Reg1 on a userform and i want to exit sub when certain numbers or unique IDs are in it and i still click the back button. The numbers are started with texts like wkx1001 , wkx2001, wkx3001 up to wkx52001.

Adding 52 if statements is too long i think. So i have decided to post it here for a simpler approach.
Thanks in advance for reading and willing to help
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
So when any of the above IDs is in the Reg1, just exit sub because they start the list.
 
Upvote 0
Hi,
if saying there are a range of numbers (IDs) that you want your code to exit if any displayed?
If so, you could get your code to check e.g.

Code:
 If Val(Reg1.Value) > 1000 And Val(Reg1.Value) < 5200 Then Exit Sub

or you could create a table with all excluded numbers where you code can make the check.

Would be more helpful if you published the code you want to amend.


Dave
 
Upvote 0
Hi,
if saying there are a range of numbers (IDs) that you want your code to exit if any displayed?
If so, you could get your code to check e.g.

Code:
 If Val(Reg1.Value) > 1000 And Val(Reg1.Value) < 5200 Then Exit Sub

or you could create a table with all excluded numbers where you code can make the check.

Would be more helpful if you published the code you want to amend.


Dave

I used this code when i had only one sheet named week1.

Code:
If Me.Reg1.Value = "wkx1001" Then Exit Sub
 
Upvote 0
You can strip the text out to evaluate the number using Mid function

Code:
If Val(Mid(Reg1.Value, 4)) > 1000 And Val(Mid(Reg1.Value, 4)) < 5200 Then Exit Sub

does this help?

Dave
 
Upvote 0
Hi,
if saying there are a range of numbers (IDs) that you want your code to exit if any displayed?
If so, you could get your code to check e.g.

Code:
 If Val(Reg1.Value) > 1000 And Val(Reg1.Value) < 5200 Then Exit Sub

or you could create a table with all excluded numbers where you code can make the check.

Would be more helpful if you published the code you want to amend.


Dave

No it did not work. I think what you have here is a rang of nimbers. I want only the numbers i showed above to be exited
 
Upvote 0
How about, assuming your code is installed in the parent object to the textbox.
Code:
If Me.Reg1.Value Like "wkx*" Then Exit Sub
 
Upvote 0
Hi,
if saying there are a range of numbers (IDs) that you want your code to exit if any displayed?
If so, you could get your code to check e.g.

Code:
 If Val(Reg1.Value) > 1000 And Val(Reg1.Value) < 5200 Then Exit Sub

or you could create a table with all excluded numbers where you code can make the check.

Would be more helpful if you published the code you want to amend.


Dave

How about, assuming your code is installed in the parent object to the textbox.
Code:
If Me.Reg1.Value Like "wkx*" Then Exit Sub

This does not move it at all
 
Upvote 0

Forum statistics

Threads
1,223,728
Messages
6,174,150
Members
452,548
Latest member
Enice Anaelle

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