【问题标题】:Excel VBA activate objects if checkbox ticked如果选中复选框,则 Excel VBA 激活对象
【发布时间】:2026-02-02 05:45:01
【问题描述】:

我有一段代码可以将输入字段从禁用更改为启用,如果相应的复选框被选中,则将颜色从灰色更改为白色。

有没有办法为所有复选框和输入字段循环或调用它,而无需为每一对单独的代码段?

我的代码是:

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

编辑:此代码在用户窗体中

【问题讨论】:

    标签: vba excel checkbox textbox


    【解决方案1】:

    更通用的方法是一个模块类

    下面是您的案例的可能应用程序,假设您的用户窗体中的 TextBoxes 和 CheckBoxes 在“名称”属性基础上配对,例如“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
      
    • 为您的项目添加一个“类模块”

      在 VBA IDE 主功能区菜单中单击“插入”->“类模块”

      或在 VBA IDE 项目窗口中的任意位置单击鼠标右键,然后在后续子菜单中选择“插入”->“类模块”

    • 在项目窗口中展开“类模块”节点

      如果您没有看到项目窗口,您可以通过单击主功能区菜单中的查看-> 项目窗口来打开它,或者按“Ctrl+R”

    • 选择您添加的新类(应该是一些“Class1”或类似的)并在属性窗口“名称”文本框中将其名称更改为“ChkBox_TxtBox_Class”

      如果您没有看到属性窗口,您可以通过单击主功能区菜单中的查看-> 属性窗口或按“F4”来打开它

    • 在类模块代码窗格中放置以下内容

          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
      
    • 运行你的例程

    【讨论】:

      【解决方案2】:

      复选框是 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”复选框除外。

      编辑:想法是复选框是一个集合,我已将您的问题翻译为“告诉我如何一起选择/选中复选框,而不是一个一个地选择”。

      在表单上应该是这样的:

      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
      

      结束子

      【讨论】:

      • 感谢您回复 Vityata。我不确定我是否跟随。我不想更改复选框的值,我想查看是否选中了复选框,如果是,则启用相应的文本框。你的脚本是怎么做到的?此外,这是在用户窗体中而不是工作表中。
      • 再次感谢 Vityata,这对我来说确实很有意义。因此,如果我希望它启用相应的文本框,我是否需要在“'在这里写你的逻辑”中使用类似的代码来依次选择它们?
      • 是的。相似的东西。取决于您的 texbox 的命名方式。例如。如果它们像“tb2”并且复选框最多为 9 个,您可以提出逻辑来读取复选框名称的最后一个字符并启用具有相似最后一个字符的标签。如果复选框超过 9 个,那么它会有点复杂 - 你必须阅读两个字符 :)
      最近更新 更多