Search code examples
for-loopeachoffice-2010

Assigning variables?


I have looked for awhile to find the answer, I see some similar codes and try to adjust my code to work. Basically, I need all textboxes on my userform to turn red with white font based on the text received from another work sheet in one of the textboxes. I know I am close, but I don't understand the placement of wording!

Sub ChangeClr() 
Dim tbMyTextBox As TextBox 
Dim ufMyUserForm As UserForm 
For Each tbMyTextBox In ufMyUserForm 
    If tbAction1 = "Tank In Spec" Then 
        tbMyTextBox.BackColor = vbGreen 
        tbMyTextBox.ForeColor = vbBlack 
    Else 
        tbTextBox.BackColor = vbGreen 
        tbMyTextBox.ForeColor = vbBlack 
    End If 
Next tbMyTextBox 
End Sub

Solution

  • Now that I know you are using VBScript in Excel, this code will loop thru and change all text boxes (including the action one) based on the wording in the action text box. You can tweak to your exact specifications.

    Dim varControl As Control
    
    
    For Each varControl In ufMyUserForm.Controls
    
     If TypeOf varControl Is MSForms.TextBox Then
             If tbAction1 = "Tank In Spec" Then
                varControl.BackColor = vbRed
                varControl.ForeColor = vbWhite
             Else
                varControl.BackColor = vbGreen
                varControl.ForeColor = vbBlack
            End If
        End If
    Next