Need help trying to colour / uncolour multiple text boxes at once in a userform

NinetalesOCE

New Member
Joined
Dec 15, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Good afternoon all

I need some help with my Userform and textboxes.

Overview Goal:
  • User opens the userform.
  • User selects a checkbox.
  • The checkbox then highlights the textboxes inside the userform that they are required to fill out.
    • Highlighted = colour of the background changes to yellow.
  • If the user clicks a second checkbox, it keeps the relevant textboxes highlighted for both checkboxes.
  • If the user unticks a checkbox, it keeps all the required checkboxes highlighted.
Step by Step:
1. User ticks checkbox1
2. Checkbox1 then highlights Textbox1, textbox3 and textbox4.
3. User ticks checkbox 3
2. Checkbox3 then Highlights textbox1, textbox 4 and textbox 6.
4. User unticks checkbox1
5. Textbox3 returns to white background, and textbox 1, 4 and 6 stay yellow.

What I have so far is sort of a brainstorm:

VBA Code:
Public yellow As String, White As String

'Creating the colours
Private Sub UserForm_Initialize()
yellow = RGB(255, 255, 0)
White = RGB(255, 255, 255)
End sub

'Calling the module
Public TestVariable(20) As MSForms.TextBox
Call TextBoxVariable
End Sub

'I was intending on doing some form of For i = (All of the values in TestValue) and then nexting through all of them to change the colours
Sub TextBoxVariable()
Set TestVariable(1) = GenerationForm.TextBox1
Set TestVariable(2) = GenerationForm.TextBox2
Set TestVariable(3) = GenerationForm.TextBox3
Set TestVariable(4) = GenerationForm.TextBox4
Set TestVariable(5) = GenerationForm.TextBox5
End Sub

'Goal was to assign a number, which is linked to the same numbered textbox as the below variable.
Private Sub CheckBox1_Click()
TestValue = Array("1", "2", "4")
For i = '(Values in TestValue)
'--Uncolour everything--
'--Recolour everything currently ticked--
TestVariable(i).BackColor = Yellow

End Sub

But as you can see, the code is a mess and i can't work out how to de-colour the non-relevant textboxes without affecting the ones that were already coloured.

Any help would be appreciated!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I think it was something like this, with Me being the userform
VBA Code:
Private Sub CheckBox1_Click()
     For i = 1 To 4
          Select Case i
               Case 3
               Case Else
                    Me("textbox" & i).BackColor = Yellow        'color textboxes
          End Select
     Next
     
     j = 10
     Me("checkbox" & j).Value = True
End Sub
 
Upvote 0
Hi,

try following & see if does what you want

Place ALL codes in your UserForms code page

VBA Code:
Private Sub CheckBox1_Click()
    TextBoxColor
End Sub

Private Sub CheckBox3_Click()
    TextBoxColor
End Sub

Private Sub TextBoxColor()
    For i = 1 To 6
        With Me.Controls("TextBox" & i)
             .BackColor = rgbWhite
            Select Case True
            Case Me.CheckBox1.Value And Me.CheckBox3.Value
                If Not IsError(Application.Match(i, Array(1, 3, 4, 6), 0)) Then .BackColor = rgbYellow
            Case Me.CheckBox1.Value
                If Not IsError(Application.Match(i, Array(1, 3, 4), 0)) Then .BackColor = rgbYellow
            Case Me.CheckBox3.Value
                If Not IsError(Application.Match(i, Array(1, 4, 6), 0)) Then .BackColor = rgbYellow
            End Select
        End With
    Next i

End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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