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:
Hmmmm, it did not move. It always exits sub

What does this mean? I tested the code and it will react to anything that is configured with more than three characters. You might need the Me.Reg1 instead of just Reg1. An overfsight on my part. But the Mod test works. It would help if you show how you have applied it in your code when you reply with failed attempts. I am lousy at mental telepathy.
 
Last edited:
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What does this mean? I tested the code and it will react to anything that is configured with more than three characters. You might need the Me.Reg1 instead of just Reg1. An overfsight on my part. But the Mod test works. It would help if you show how you have applied it in your code when you reply with failed attempts. I am lousy at mental telepathy.

Sorry for the hmmm.
This is how i applied it:

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)

If CLng (Mid(Me.Reg1.Value, 4)) Mod 1000 <> 1 Then Exit Sub

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
Apparently you missed post #25 where I made a correction. The correct statement is
Code:
If CLng(Mid(Reg1.Value, 4)) Mod 1000 [COLOR=#ff0000]=[/COLOR] 1 Then Exit Sub

Note that it is 'Equal' instead of 'NotEqual'
 
Last edited:
Upvote 0
I did actually miss it. Now it is working perfectly! And i dont even know what to say. Just wanna say thank you for not giving up on me.
 
Last edited:
Upvote 0
Happy to help. For future reference. Post you pertinent code in the original post and also post any images of your worksheet, screen shots or mock ups that will help the responders understand what you are working with. We don't want your data, except to understand your objectives, so phony data is okay as long as it is in the same locations as your actual data. This thread could probably have been resolved in less than 10 postings if you had done those things originally. See the sentence with colored font in my signature line below for adding attachments.
Regards, JLG
 
Last edited:
Upvote 0
Happy to help. For future reference. Post you pertinent code in the original post and also post any images of your worksheet, screen shots or mock ups that will help the responders understand what you are working with. We don't want your data, except to understand your objectives, so phony data is okay as long as it is in the same locations as your actual data. This thread could probably have been resolved in less than 10 postings if you had done those things originally. See the sentence with colored font in my signature line below for adding attachments.
Regards, JLG

Thanks for the info. I am learning how to add attachments and i believe next time i will be through.

Thanks again
 
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