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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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

So it is like this: stop when the ID in Reg1 equals the data in cell B3 because the IDs start from B3
 
Upvote 0
So it is like this: stop when the ID in Reg1 equals the data in cell B3 because the IDs start from B3

I think you are going to have to put some images of your worksheet, userform, etc. where we can see them and explain where you are running the code from. The feedback is not sufficient to give good adivice.
 
Upvote 0
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

Then my other suggestion of creating a table where you can add numbers to be excluded & get your code to check that could do what you want?

Untested but something like following maybe:

Rich (BB code):
     Dim rng As Range, m As Variant    
     Set rng = Sheets("Sheet1").Columns(1)
    m = Application.Match(Me.Reg1.Value, rng, False)
    If Not IsError(m) Then Exit Sub

You place all excluded numbers in column 1 of worksheet (change name as required) & if match found, exits your sub.

Dave
 
Last edited:
Upvote 0
Okay i think this one has solved the problem. Wow thanks for the time , energy and patience with me. I am most grateful
 
Upvote 0
Okay i think this one has solved the problem. Wow thanks for the time , energy and patience with me. I am most grateful

most welcome - many thanks for feedback very much appreciated.

Dave
 
Upvote 0
It worked when the sheet named week_01 is selected. But for the other sheets it does not.

Did you say i should place in column 1 of all the sheets or on just one sheet should have all the numbers to be excluded??
 
Upvote 0
It worked when the sheet named week_01 is selected. But for the other sheets it does not.

Did you say i should place in column 1 of all the sheets or on just one sheet should have all the numbers to be excluded??

You have one reference sheet (Sheet1 or whatever you name it) & in Column 1 list all the numbers you want excluded. Entry is case sensitive.

Dave
 
Upvote 0
Before, i modified it to
Code:
     Dim sht As String 
      sht = ComboBox1.Value
     Dim rng As Range, m As Variant    
     Set rng = Sheets(sht).Columns(1)
    m = Application.Match(Me.Reg1.Value, rng, False)
    If Not IsError(m) Then Exit Sub

Then i placed the numbers i column 1 of each sheet and one the week_01 sheet when selected in the combobox it worked fine but the rest did not exit sub.


Now when i have it this way
Code:
Dim rng As Range, m As Variant    
     Set rng = Sheets("Sheet2").Columns(1)
    m = Application.Match(Me.Reg1.Value, rng, False)
    If Not IsError(m) Then Exit Sub

And place all numbers to exclude it does not work fine. It always exits sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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