查找复选框的 true/false 状态

Find the true/false status of a checkbox

我在用户表单中有以下内容。它应该从已选中的组框中的每个复选框的命名范围中提取简洁代码,然后将其打印到工作表中所需的单元格中。

Dim AirExGroup As Frame
Dim TrkExGroup As Frame
Dim OthrExgroup As Frame
Dim cb As Control

Set AirExGroup = Me.AirExGrp        'Contains all Air exception checkboxes
Set TrkExGroup = Me.TruckExGrp      'Contains all Truck exception checkboxes
Set OthrExgroup = Me.OthrExGrp      'Contains all Other exception checkboxes
   
'Airline Exceptions
For Each cb In AirExGroup.Controls
    If TypeName(cb) = "CheckBox" Then
        If cb.ControlFormat.Value = 1 Then
            'If checked, print Brevity code from Air_Ex list to Air Exception Column
        Else
            'If no boxes checked, print "N/A" in exceptions cell
    End If
  End If
Next cb

'Trucker Exceptions
For Each cb In TrkExGroup.Controls
    If TypeName(cb) = "CheckBox" Then
        If cb.ControlFormat.Value = 1 Then
             'If checked, print Brevity code from Trk_Ex list
        Else
             'If no boxes checked, print "N/A" in exceptions cell
    End If
  End If
Next cb

'Warehouse Exceptions
For Each cb In OthrExgroup.Controls
    If TypeName(cb) = "CheckBox" Then
        If cb.ControlFormat.Value = 1 Then
            'If checked, print Brevity code from Othr_Ex list
        Else
            'If no boxes checked, print "N/A" in exceptions cell
        End If
    End If
Next cb`

我不知道如何获取检查哪些框被选中的部分,从代码列表中提取正确的简洁代码并将它们打印到工作表上的正确单元格中。

下面的代码将该数据加载到数组中,但我无法让两者相互通信。
我找到了检查一组复选框是真还是假以及从数组中提取数据的解决方案。
我在这里没有找到任何讨论两者协同工作的内容。

Dim Exceptions(1 To 8, 1 To 2) As String
Dim i As Integer, j As Integer
Dim cb As Shape

For i = 2 To 8
    For j = 1 To 2
        Exceptions(i, j) = Worksheets("List_Data").Cells(i, j).Value
    Next j
Next i

MsgBox Exceptions(4, 1) & ", " & Exceptions(6, 1)

----编辑----
我可能已经开始弄明白了,但我明白了

"Object doesn't support this property or method"

调试指向 If cb.ControlFormat.Value = True。关于使用什么语法来查找组框中复选框的 true/false 状态有什么建议吗?

Set AirCBCap = Worksheets("List_Data").Range("B2") 'Define checkbox caption reference column
Set Air_Ex = Worksheets("List_Data").Range("A2")  'Define brevity code list reference

For Each cb In AirExGroup.Controls
If TypeName(cb) = "CheckBox" Then
    If cb.ControlFormat.Value = True Then
        For i = 0 To 6
            If cb.Caption = AirCBCap.Offset(i, 0).Value Then 'If checked, find offset in caption name list that matches the checkbox caption     
                If AirCode = "" Then
                    AirCode = Air_Ex.Offset(i, 0)  'Find the offset that matches the offset of the checkbox caption and apply that to "AirCode"
                Else
                    AirCode = AirCode & ", " & Air_Ex.Offset(i, 0).Value
                End If
            End If
        Next i
    Else
        AirCode = "N/A" 'If no boxes checked, print "N/A" in exceptions cell
    End If
End If
Next cb 
emptyCell.Offset(0, 13).Value = AirCode

我能够使用 For / Next 循环使其运行。它不是很漂亮,因为有很多 Dims 和 Set 范围,我确信可以将它们组合成更优雅的东西,但它按照我想要的方式工作,所以我现在就接受它。

Dim emptyCell As Range, AirBoxList As Range, TrkBoxList As Range, WhsBoxList As Range, AirCBCap As Range, Air_Ex As Range
Dim TrkCBCap As Range, WhsCBCap As Range, Trk_Ex As Range, Whs_Ex As Range
Dim shtSel As String, AirBoxName As String, AirCode As String, TrkCode As String, TrkBoxName As String, WhsCode As String
Dim WhsBoxName As String
Dim AirExGroup As Frame, TrkExGroup As Frame, WhseExgroup As Frame
Dim i As Integer

shtSel = sheetslistCB.Value

'Make selected sheet active
Worksheets(shtSel).Activate

Set AirExGroup = Me.AirlineExGrp    'Contains all Airline exception checkboxes
Set TrkExGroup = Me.TruckExGrp      'Contains all Trucker exception checkboxes
Set WhseExgroup = Me.WhseExGrp      'Contains all Warehouse exception checkboxes

Set emptyCell = Worksheets(shtSel).Range("A6") 'SelectFirstBlankCell Use function to determine emptyRow
Set AirCBCap = Worksheets("List_Data").Range("B2") 'Define checkbox caption reference column
Set Air_Ex = Worksheets("List_Data").Range("A2")  'Define brevity code list reference
Set AirBoxList = Worksheets("List_Data").Range("M2") 'Define list of names of Air freight check boxes
Set TrkBoxList = Worksheets("List_Data").Range("N2") 'Define list of names of Truck Check Boxes
Set WhsBoxList = Worksheets("List_Data").Range("O2") 'Define list of names of Warehouse check boxes
Set TrkCBCap = Worksheets("List_Data").Range("D2")   'Define checkbox caption reference column (Trucking)
Set WhsCBCap = Worksheets("List_Data").Range("F2")   'Define checkbox caption reference column (Warehouse)
Set Trk_Ex = Worksheets("List_Data").Range("C2")     'Define Brevity code list reference (Trucking)
Set Whs_Ex = Worksheets("List_Data").Range("E2")     'Define Brevity code list reference (Warehouse)

'Airline Exceptions
For i = 0 To 6
    AirBoxName = AirBoxList.Offset(i, 0).Value
    If Me.Controls(AirBoxName).Value = True Then
        If Me.Controls(AirBoxName).Caption = AirCBCap.Offset(i, 0).Value Then
            If AirCode = "" Then
                AirCode = Air_Ex.Offset(i, 0).Value 'Find the offset that matches the offset of the checkbox caption and apply that to "AirCode"
            Else
                AirCode = AirCode & ", " & Air_Ex.Offset(i, 0).Value
            End If
         End If
    End If
Next i
emptyCell.Offset(0, 13).Value = AirCode
'Trucker Exceptions
For i = 0 To 3
    TrkBoxName = TrkBoxList.Offset(i, 0).Value
    If Me.Controls(TrkBoxName).Value = True Then
        If Me.Controls(TrkBoxName).Caption = TrkCBCap.Offset(i, 0).Value Then
            If TrkCode = "" Then
                 TrkCode = Trk_Ex.Offset(i, 0).Value 'Find the offset that matches the offset of the checkbox caption and apply that to "TrkCode"
            Else
                TrkCode = TrkCode & ", " & Trk_Ex.Offset(i, 0).Value
            End If
        End If
    End If
Next i
emptyCell.Offset(0, 14).Value = TrkCode

'Warehouse Exceptions
For i = 0 To 0
    WhsBoxName = WhsBoxList.Offset(i, 0).Value
    If Me.Controls(WhsBoxName).Value = True Then
        If Me.Controls(WhsBoxName).Caption = WhsCBCap.Offset(i, 0).Value Then
            If WhsCode = "" Then
                WhsCode = Whs_Ex.Offset(i, 0).Value 'Find the offset that matches the offset of the checkbox caption and apply that to "WhsCode"
            Else
                WhsCode = WhsCode & ", " & Whs_Ex.Offset(i, 0).Value
            End If
        End If
    End If
Next i
emptyCell.Offset(0, 15).Value = WhsCode

Unload Me

End Sub