VBA – 请参阅单元格地址中的checkbox

我循环了几行,我需要知道每行中的checkbox是否是“Checked”,但我不知道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 

要创buildcheckbox,我做了以下操作:

  1. 创build一个checkbox
  2. 把它放在一个单元格中
  3. 复制下面在同一列

我认为代码应该是完全相反的:

 CheckBox1.LinkedCell 

尝试….

 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 

这是一个很好的解决方法。 代码将所有checkbox链接到它们所在的单元格,并为它们提供布尔值(TRUE / FALSE)。 对于视觉外观,我使用了“数字形成”,使文本“真/假”不可见。 所有你需要做的就是调用函数与工作表(checkbox在哪里)作为input。 这个想法来自埃涅阿斯

 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