VBA code to enable textbox based on multiple conditions

jptaz

New Member
Joined
May 1, 2020
Messages
46
Office Version
  1. 2010
Platform
  1. Windows
Hello, I have this code to enable and change font of multiple textboxes based on one condition. However I'd like to add a condition and I don't know how to write this to make sense in my code. If you can help me it would be great.

thanks for your time

VBA Code:
Private Sub Reg351_Change()

Dim ind As Long
For ind = 446 To 485
Controls("Reg" & ind).Enabled = Controls.Item("Reg351").Text = "condition1" 'I'd like to add condition2 here 

Controls("Reg" & ind).BackColor = IIf(Controls.Item("Reg351").Text = "condition1", "&H80000005", "&H80000004") 'I'd like to add condition2 here 



Next ind

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hello Jptaz,
It's hard to understand what you are actually want to achieve.
Here is code I suppose you need. We can't help you if you don't describe problem well.
If this not help you, do not to be lazy, try to give us more details.
We more time spending trying to understand problem instead helping you.
VBA Code:
Private Sub Reg351_Change()
    
    Dim ind As Long
   
    For ind = 446 To 485
        If Controls.Item("Reg351").Text = "condition1" Then
            Controls("Reg" & ind).BackColor = &H80000005
            Controls("Reg" & ind).Enabled = True
        Else
            Controls("Reg" & ind).BackColor = &H80000004
            Controls("Reg" & ind).Enabled = False
        End If
    Next ind
    
End Sub
 
Upvote 0
Hello Jptaz,
It's hard to understand what you are actually want to achieve.
Here is code I suppose you need. We can't help you if you don't describe problem well.
If this not help you, do not to be lazy, try to give us more details.
We more time spending trying to understand problem instead helping you.
VBA Code:
Private Sub Reg351_Change()
   
    Dim ind As Long
  
    For ind = 446 To 485
        If Controls.Item("Reg351").Text = "condition1" Then
            Controls("Reg" & ind).BackColor = &H80000005
            Controls("Reg" & ind).Enabled = True
        Else
            Controls("Reg" & ind).BackColor = &H80000004
            Controls("Reg" & ind).Enabled = False
        End If
    Next ind
   
End Sub
Hello and thank you for your answer, I'm sorry if I wasn't clear. What I actually want is to change the backColor and enabling said textboxes if the text in "Reg351" = to "condition1" OR "condition2"

My reflex would be to write it like below, but I get an error message.

Controls("Reg" & ind).Enabled = Controls.Item("Reg351").Text = "condition1" or "condition2"

thanks
 
Upvote 0

Forum statistics

Threads
1,224,832
Messages
6,181,231
Members
453,026
Latest member
cknader

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