Excel VBA 如果勾选复选框则激活对象
Excel VBA activate objects if checkbox ticked
我有一段代码可以将输入字段从禁用更改为启用,如果勾选相应的复选框,则将颜色从灰色更改为白色。
有没有一种方法可以为所有复选框和输入字段循环或调用它,而无需为每一对单独编写一段代码?
我的代码是:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
tb01.Enabled = True
tb01.BackColor = vbWhite
Else: tb01.Enabled = False
tb01.BackColor = vb3DLight
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
tb02.Enabled = True
tb02.BackColor = vbWhite
Else: tb02.Enabled = False
tb02.BackColor = vb3DLight
End If
End Sub
编辑:此代码在用户窗体中
复选框是Excel中的一个集合,你可以这样做:
Sub SelectCheckboxes()
Dim CB As CheckBox
For Each CB In Sheet1
If CB.Name <> Sheet1.CheckBoxes("SkipThisCheckbox").Name Then
CB.Value = 2
End If
Next CB
End Sub
您将值更改为所有复选框,"SkipThisCheckbox" 复选框除外。
编辑:想法是复选框是一个集合,我已将您的问题翻译为 "show me how to select/check checkboxes together and not one by one"。
在表单上应该是这样的:
Private Sub CommandButton1_Click()
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "Checkbox" Then
Debug.Print cCont
'True or False if checked
'Write your logic here.
End If
Next cCont
结束子
更通用的方法是一个模块class一个
此处是针对您的案例的可能应用,假设您的用户窗体中的文本框和复选框在 "Name" 属性 基础上配对,例如 "CheckBox1"-"TextBox1", "CheckBox2"-"TextBox2", ...
在用户窗体代码窗格中放置以下代码
Option Explicit
Dim chkBoxes(1 To 4) As ChkBox_TxtBox_Class 'array of type "ChkBoxClass" which you define in a Class Module
Private Sub UserForm_Initialize()
Dim nControls As Integer, i As Integer
nControls = 4 '<== set here the number of CheckBox-TextBox pairs you have in the Form
For i = 1 To nControls
Set chkBoxes(i) = New ChkBox_TxtBox_Class 'initialize a new instance of 'ChkBoxClass' class and store it in the array i-th position
With chkBoxes(i)
Set .ChkBox = Me.Controls("CheckBox" & i) 'assign the correct CheckBox control to its ChkBox property
Set .TxtBox = Me.Controls("TextBox" & i) 'assign the correct TextBox control to its TxtBox property
.ChkBox.value = False 'set "unchecked" as checkbox initial value
.SetTexBox ' call the class method that will have the "paired" textbox adjust its state accordingly to checkbox value
End With
Next i
End Sub
向您的项目添加一个 "Class Module"
点击 VBA IDE 主功能区菜单中的插入-> Class 模块
或右键单击 VBA IDE 项目 Window 中的任意位置,然后 select 在后续子菜单中插入 -> Class 模块
展开项目中的"Class Module"节点Window
如果您没有看到项目 Window,您可以通过单击主功能区菜单中的查看-> 项目 Window 打开它,或者按 "Ctrl+R"
select 你添加的新Class(应该是一些"Class1"之类的)并在属性 Window "Name" 文本框
如果您没有看到 属性 Window,您可以通过单击主功能区菜单中的查看-> 属性 Window 或按 "F4"
在 Class 模块代码窗格中放置以下内容
Option Explicit
'declare class properties of CheckBox and TextBox type to be "paired" in every instance of this class. the CheckBox one will be associated to events
Public WithEvents ChkBox As MSForms.CheckBox ' ChkBox is a property of the class of type CheckBox. it's associated to events
Public TxtBox As MSForms.TextBox ' TxtBox is a property of the class of type TextBox
' events associated to ChkBox class property
Sub ChkBox_Change()
Call SetTexBox 'call the "method" (i.e. a Sub attached to the class) associated to ChkBox property state change
End Sub
Sub SetTexBox()
'sub that sets the state of the associated TextBox accordingly to the state of the associated CheckBox
With Me.ChkBox
If .value Then
TxtBox.Enabled = True
TxtBox.BackColor = vbWhite
Else
TxtBox.Enabled = False
TxtBox.BackColor = vb3DLight
End If
End With
End Sub
运行你的例程
我有一段代码可以将输入字段从禁用更改为启用,如果勾选相应的复选框,则将颜色从灰色更改为白色。
有没有一种方法可以为所有复选框和输入字段循环或调用它,而无需为每一对单独编写一段代码?
我的代码是:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
tb01.Enabled = True
tb01.BackColor = vbWhite
Else: tb01.Enabled = False
tb01.BackColor = vb3DLight
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
tb02.Enabled = True
tb02.BackColor = vbWhite
Else: tb02.Enabled = False
tb02.BackColor = vb3DLight
End If
End Sub
编辑:此代码在用户窗体中
复选框是Excel中的一个集合,你可以这样做:
Sub SelectCheckboxes()
Dim CB As CheckBox
For Each CB In Sheet1
If CB.Name <> Sheet1.CheckBoxes("SkipThisCheckbox").Name Then
CB.Value = 2
End If
Next CB
End Sub
您将值更改为所有复选框,"SkipThisCheckbox" 复选框除外。
编辑:想法是复选框是一个集合,我已将您的问题翻译为 "show me how to select/check checkboxes together and not one by one"。
在表单上应该是这样的:
Private Sub CommandButton1_Click()
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "Checkbox" Then
Debug.Print cCont
'True or False if checked
'Write your logic here.
End If
Next cCont
结束子
更通用的方法是一个模块class一个
此处是针对您的案例的可能应用,假设您的用户窗体中的文本框和复选框在 "Name" 属性 基础上配对,例如 "CheckBox1"-"TextBox1", "CheckBox2"-"TextBox2", ...
在用户窗体代码窗格中放置以下代码
Option Explicit Dim chkBoxes(1 To 4) As ChkBox_TxtBox_Class 'array of type "ChkBoxClass" which you define in a Class Module Private Sub UserForm_Initialize() Dim nControls As Integer, i As Integer nControls = 4 '<== set here the number of CheckBox-TextBox pairs you have in the Form For i = 1 To nControls Set chkBoxes(i) = New ChkBox_TxtBox_Class 'initialize a new instance of 'ChkBoxClass' class and store it in the array i-th position With chkBoxes(i) Set .ChkBox = Me.Controls("CheckBox" & i) 'assign the correct CheckBox control to its ChkBox property Set .TxtBox = Me.Controls("TextBox" & i) 'assign the correct TextBox control to its TxtBox property .ChkBox.value = False 'set "unchecked" as checkbox initial value .SetTexBox ' call the class method that will have the "paired" textbox adjust its state accordingly to checkbox value End With Next i End Sub
向您的项目添加一个 "Class Module"
点击 VBA IDE 主功能区菜单中的插入-> Class 模块
或右键单击 VBA IDE 项目 Window 中的任意位置,然后 select 在后续子菜单中插入 -> Class 模块
展开项目中的"Class Module"节点Window
如果您没有看到项目 Window,您可以通过单击主功能区菜单中的查看-> 项目 Window 打开它,或者按 "Ctrl+R"
select 你添加的新Class(应该是一些"Class1"之类的)并在属性 Window "Name" 文本框
如果您没有看到 属性 Window,您可以通过单击主功能区菜单中的查看-> 属性 Window 或按 "F4"
在 Class 模块代码窗格中放置以下内容
Option Explicit 'declare class properties of CheckBox and TextBox type to be "paired" in every instance of this class. the CheckBox one will be associated to events Public WithEvents ChkBox As MSForms.CheckBox ' ChkBox is a property of the class of type CheckBox. it's associated to events Public TxtBox As MSForms.TextBox ' TxtBox is a property of the class of type TextBox ' events associated to ChkBox class property Sub ChkBox_Change() Call SetTexBox 'call the "method" (i.e. a Sub attached to the class) associated to ChkBox property state change End Sub Sub SetTexBox() 'sub that sets the state of the associated TextBox accordingly to the state of the associated CheckBox With Me.ChkBox If .value Then TxtBox.Enabled = True TxtBox.BackColor = vbWhite Else TxtBox.Enabled = False TxtBox.BackColor = vb3DLight End If End With End Sub
运行你的例程