【问题标题】:VBA - Refer to CheckBox from the cell addressVBA - 从单元格地址引用 CheckBox
【发布时间】:2017-06-13 06:38:35
【问题描述】:

我正在遍历几行,我需要知道每行中的 CheckBox 是否“已选中”,但我不知道 CheckBox 的名称。下面的代码只是为了说明问题:

Sub Checkboxes()

Dim ws As Worksheet
Set ws = Sheets("Input Data")
Dim Switch As Boolean

For i = 4 To 8
    Switch = ws.Cells(i, 11).CheckboxValue
    MsgBox Switch
Next i


End Sub

要创建复选框,我做了以下操作:

  1. 创建一个复选框
  2. 将其放入单元格中
  3. 在同一列中复制以下内容

我认为代码应该与以下内容完全相反:

CheckBox1.LinkedCell

【问题讨论】:

  • 首先,您的代码循环遍历列而不是行,其次,在开发人员选项卡下创建的复选框是未连接到工作表单元格的单独对象
  • 感谢您注意到循环错误。对于您的第二点:您能提出解决方法吗?

标签: vba excel checkbox


【解决方案1】:

这是一个很好的解决方法。该代码将所有 CheckBox 链接到它们所在的单元格,并为它们提供布尔值 (TRUE/FALSE)。对于视觉外观,我使用了“数字格式”,使文本“真/假”不可见。您需要做的就是使用工作表(复选框所在的位置)作为输入调用该函数。想法来自Aeneas

Public Function Link_Checkboxes_To_Cells(ws As Worksheet)

'This function is linking the checkboxes to the cells that they are in, so that the value of the cell becomes TRUE/FALSE when using the checkbox within.
'Meanwhile, I have manually made the text invisible in the cells with checkboxes, using the following method:
' https://support.office.com/en-us/article/Hide-or-display-cell-values-c94b3493-7762-4a53-8461-fb5cd9f05c33#bm1
' Number Type ---> Custom --> Then type ";;;" (without the quotes) and OK

Dim chk As CheckBox

For Each chk In ws.Checkboxes
   With chk
      .LinkedCell = _
         .TopLeftCell.Offset(0, 0).Address
   End With
Next chk

End Function

【讨论】:

    【解决方案2】:

    试试....

    Sub Checkboxes()
    
        Dim ws As Worksheet
        Set ws = Sheets("Input Data")
        Dim Switch As Boolean
    
        For Each cb In ws.Checkboxes
            If cb.Value = 1 Then
                Switch = True
            Else
                Switch = False
            End If
    
            MsgBox cb.Name & " Value= " & Switch
    
        Next cb
    
    End Sub
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-10
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多