VBA-如何在这种情况下应用循环(Userform.Combox ...)?

VBA-如何在这种情况下应用循环(Userform.Combox ...)?

我试过“For i=1”等...但没有用,我只是不知道语法。你能帮我把这段代码放到“For i=1 to 33...”中吗?

    ColorUpdateBoxes = RGB(255, 255, 180)
    With UserForm9
       .ComboBox1.BackColor = ColorUpdateBoxes
       .ComboBox2.BackColor = ColorUpdateBoxes
       .ComboBox3.BackColor = ColorUpdateBoxes
       .ComboBox4.BackColor = ColorUpdateBoxes
       .ComboBox5.BackColor = ColorUpdateBoxes
       .ComboBox6.BackColor = ColorUpdateBoxes
       .ComboBox7.BackColor = ColorUpdateBoxes
       .ComboBox8.BackColor = ColorUpdateBoxes
       .ComboBox9.BackColor = ColorUpdateBoxes
       .ComboBox10.BackColor = ColorUpdateBoxes
       .ComboBox11.BackColor = ColorUpdateBoxes
       .ComboBox12.BackColor = ColorUpdateBoxes
       .ComboBox13.BackColor = ColorUpdateBoxes
       .ComboBox14.BackColor = ColorUpdateBoxes
       .ComboBox15.BackColor = ColorUpdateBoxes
       .ComboBox16.BackColor = ColorUpdateBoxes
       .ComboBox17.BackColor = ColorUpdateBoxes
       .ComboBox18.BackColor = ColorUpdateBoxes
       .ComboBox19.BackColor = ColorUpdateBoxes
       .ComboBox20.BackColor = ColorUpdateBoxes
       .ComboBox21.BackColor = ColorUpdateBoxes
       .ComboBox22.BackColor = ColorUpdateBoxes
       .ComboBox23.BackColor = ColorUpdateBoxes
       .ComboBox24.BackColor = ColorUpdateBoxes
       .ComboBox25.BackColor = ColorUpdateBoxes
       .ComboBox26.BackColor = ColorUpdateBoxes
       .ComboBox27.BackColor = ColorUpdateBoxes
       .ComboBox28.BackColor = ColorUpdateBoxes
       .ComboBox29.BackColor = ColorUpdateBoxes
       .ComboBox30.BackColor = ColorUpdateBoxes
       .ComboBox31.BackColor = ColorUpdateBoxes
       .ComboBox32.BackColor = ColorUpdateBoxes
       .ComboBox33.BackColor = ColorUpdateBoxes
    End With

答案1

使用For Each循环。

Dim c As Control

For Each c In Me.Controls
    If TypeName(c) = "ComboBox" Then
        c.BackColor = ColorUpdateBoxes
    End If
Next

相关内容