VBA Loop through textbox controls in userform

后端 未结 1 1540
自闭症患者
自闭症患者 2021-01-27 13:23

I have looked through numerous posts on looping through UserForm Controls but cant seem to adjust the code i have found for my needs and need some help.

Scenario I am tr

1条回答
  •  孤街浪徒
    2021-01-27 14:10

    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
    

    0 讨论(0)
提交回复
热议问题