我已经浏览了许多有关循环用户窗体控件的帖子,但似乎无法调整我找到的代码来满足我的需要,并且需要一些帮助。
我想弄清楚的场景:
我的用户表单上有 44 个文本框,其名称均以“ch”开头,例如“chTextBox1”
当用户窗体激活时,我需要循环所有以“ch”开头的文本框,并将这些文本框的背景颜色更改为基于单元格内部颜色的颜色
下面是我一直在搞乱的代码,我要么陷入无限循环,要么得到
错误424
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 JHKey.Controls
If TypeName(c) = "TextBox" Then
y = Left(c, 2)
End If
If y = "ch" Then
c.BackColor = bColor.Interior.Color
End If
Next c
End Sub
尝试将“ch”的 If 语句测试放在“TextBox”的 If 语句测试中。另外,在检查其名称时,应指定控件的 Name 属性,否则将默认为其 Value 属性。另外,顺便说一句,我建议用关键字 Me 替换 JHKey,它指的是用户表单本身,无论其名称如何。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)