且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBA通过用户窗体中的文本框控件循环

更新时间:2021-08-01 02:46:52

尝试将"ch"的If语句测试放入"TextBox"的If语句测试中.另外,在检查控件名称时,应为控件指定Name属性,否则默认为其Value属性.另外,顺便说一句,我建议用关键字Me替换JHKey,该关键字指的是用户窗体本身,而不管其名称如何.

Try placing the If statement testing for "ch" within the If statement testing for "TextBox". Also, you should specify the Name property for the control when checking for its name, otherwise it defaults to its Value property. Also, as an aside, I would suggest replacing JHKey with the keyword Me, which refers to the userform itself regardless of its name.

Private Sub UserForm_Activate()
    Dim wb As Workbook
    Dim wsRR As Worksheet
    Dim bColor As Range
    Dim c As Control
    Dim y As String

    Set wb = Application.ThisWorkbook
    Set wsRR = wb.Sheets("RiskRating")
    Set bColor = wsRR.Range("C3")

    For Each c In Me.Controls
        If TypeName(c) = "TextBox" Then
            y = Left(c.Name, 2)
            If y = "ch" Then
                c.BackColor = bColor.Interior.Color
            End If
        End If
    Next c
End Sub