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:
Suggestion was a build a single table with all excluded numbers that your code checked against.

Issue with Application.Match could be a case sensitivity issue

Try this update using find function:

Code:
     Dim rng As Range
    Set rng = Sheets("Sheet2").Columns(1).Find(Me.Reg1.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If Not rng Is Nothing Then Exit Sub

Dave
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Okay. My power is out at the moment. Will test it when the power is back. Thanks very much
 
Upvote 0
Suggestion was a build a single table with all excluded numbers that your code checked against.

Issue with Application.Match could be a case sensitivity issue

Try this update using find function:

Code:
     Dim rng As Range
    Set rng = Sheets("Sheet2").Columns(1).Find(Me.Reg1.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If Not rng Is Nothing Then Exit Sub

Dave

Still it is not working.
 
Upvote 0
Suggestion was a build a single table with all excluded numbers that your code checked against.

Issue with Application.Match could be a case sensitivity issue

Try this update using find function:

Code:
     Dim rng As Range
    Set rng = Sheets("Sheet2").Columns(1).Find(Me.Reg1.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If Not rng Is Nothing Then Exit Sub

Dave

Not yet. I dont know how to place the copy there. I am on mobile phone. And when i try it does not work
 
Upvote 0
I think giving the actual code i have here will help.
Code:
Private Sub CmdBackData_Click()
  Dim FindRow As Range, cRow As String, cNum As Integer, x As Integer, sht As String
sht = ComboBox1.Value

On Error Resume Next

cRow = Me.Reg1.Value
Set FindRow = Sheets(sht).Range("B3:B303").Find(What:=cRow, LookIn:=xlValues).Offset(-1, 0)
'I use this to exit the sub but i want it short
If Me.Reg1.Value = "WKX1001" Then Exit Sub
If Me.Reg1.Value = "WKX2001" Then Exit Sub
If Me.Reg1.Value = "WKX3001" Then Exit Sub
If Me.Reg1.Value = "WKX4001" Then Exit Sub
'This goes on up to WKX52001 and makes the code too long so i want i short version
cNum = 11
For x = 1 To cNum
Me.Controls("Reg" & x ).Value = FindRow.Text
Set FindRow = FindRow.Offset(0, 1)

Next

On Error GoTo 0
End Sub
 
Upvote 0
I need someone to look at this code for me in order to make it short. Thanks
 
Upvote 0
I need someone to look at this code for me in order to make it short. Thanks

You can replace all this
Code:
If Me.Reg1.Value = "WKX1001" Then Exit Sub
If Me.Reg1.Value = "WKX2001" Then Exit Sub
If Me.Reg1.Value = "WKX3001" Then Exit Sub
If Me.Reg1.Value = "WKX4001" Then Exit Sub
'This goes on up to WKX52001 and makes the code too long so i want i short version

With this

Code:
If CLng(Mid(Reg1.Value, 4)) Mod 1000 <> 1 Then Exit Sub
 
Upvote 0
Sorry, should have been

Code:
If CLng(Mid(Reg1.Value, 4)) Mod 1000 = 1 Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
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