disable textboxes when conditions are met

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello All,
I have this challenge here: i wanna find out if what i am thinking is possible. Then after that i need help with it.

So i have a textbox called txtCurrentUser and in this textbox i have username in it. Now i wanna write a script to look at the txtCurrentUser, counting from the left to right, match the third character to another textbox's value(txtOption). If value is not the same, then disable all textboxes from Rw2 to Rw23 (these are the names of textboxes i wanna disable if names didnt match). The value in txtCurrentUser could be as:
M1Afighter
M2Bswimmer
M3Csinger
M4Dpainter
M5 fisher

But when the values are the same, then i will like to disable all but two textboxes. Say i have the username: M5 fisher then in txtOption there is blank, then disable all except two say Rw6 and Rw16. Else disable all of them.

So the A, B, C, D or Blank is what i am talking about as the third character in the txtCurrentUser. Thanks and i hope this is very clear.
Kelly

Ps.
All these controls are on a userform
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I would normally ask my question like:
If this happens do this if not do this.

But you asked your question like this:

If this does not happen do this but if this does happen do this.


If my child was talking too much I may say this:

"If you speak one more time your going to bed" and I would think he could understand this.

But If I said: "If you do not speak one more time your going to stay up". I would think he would have to think a little longer to figure out what that may mean.

Just a thought of mine.

I worked on this for a little while but my head started hurting figuring out all the not and if not's

But this is probable because my Brain is just too simple.
I hope someone else will jump in here and help you
 
Upvote 0
I think this loop will do what you want

Code:
For i = 2 to 23
    Me.Controls("Rw" & i).Enabled = False
Next i

Rw6.Enabled = (LCase(Mid(txtCurrentUser, 3, 1)) = LCase(txtOption.Text))
Rw16.Enabled = Rw6.Enabled


But this is probable because my Brain is just too simple.
That's what happens when you start thinking like a computer. :)
 
Last edited:
Upvote 0
Wow!!!! Cant believe this but i have to. Worked like a charm. Thanks a lot. I need some more guidance here: the textboxes from Rw6 to Rw23 are 18 in number and pairing them gives me 9 pairs. And the thing is when the fourth character in txtCurrentUser is say "f" , i will enable only a pair of the "Rw" & i. So when txtCurrentUser contains say "p" as the 4th character, then another pair is enabled and so on. So this time we looking at the txtCurrentUser and txtOption for the match of the 3rd character then back to the txtCurrentUser to tell which of the Rw & i to enable based on the 4th character in it.

I should have added this in the first place but did not see the image clearly then. Thanks again.
Kelly
 
Upvote 0
I would normally ask my question like:
If this happens do this if not do this.

I went through a lot of headaches myself to be able to put this post out here. Haha. Thanks for the response.
Kelly
 
Last edited:
Upvote 0
Wow!!!! Cant believe this but i have to. Worked like a charm. Thanks a lot. I need some more guidance here: the textboxes from Rw6 to Rw23 are 18 in number and pairing them gives me 9 pairs. And the thing is when the fourth character in txtCurrentUser is say "f" , i will enable only a pair of the "Rw" & i. So when txtCurrentUser contains say "p" as the 4th character, then another pair is enabled and so on. So this time we looking at the txtCurrentUser and txtOption for the match of the 3rd character then back to the txtCurrentUser to tell which of the Rw & i to enable based on the 4th character in it.

I should have added this in the first place but did not see the image clearly then. Thanks again.
Kelly
What adaptations of my code have you tried?

You might try using something like

Code:
For i = 2 to 23
    Me.Controls.("Rw" & i).Enabled = CBool((i And 2) And 3)
Next I
 
Last edited:
Upvote 0
What adaptations of my code have you tried?

You might try using something like

Code:
For i = 2 to 23
    Me.Controls("Rw" & i).Enabled = CBool((i And 2) And 3)
Next I


This is how i adapted to the code and how it is turn the textboxes on and off is not cool with me.
So what actually does the CBool((i And 2) And 3) do?

Code:
For i = 2 to 23
    Me.Controls("Rw" & i).Enabled = CBool((i And 2) And 3)
Next I
Rw6.Enabled = (LCase(Mid(txtCurrentUser, 3, 1)) = LCase(txtOption.Text))
Rw16.Enabled = Rw6.Enabled
 
Last edited:
Upvote 0
what if i want to disable like this: when the fourth character in txtCurrentUser is say "f" , i will enable only a pairs say Rw6 and Rw15. Then when txtCurrentUser contains say "p" as the 4th character, then another pair say Rw7 and Rw16 is enabled and so on?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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