在PowerPoint VBA中查找数组中的最低值、次低值和最高值

在PowerPoint VBA中查找数组中的最低值、次低值和最高值,vba,powerpoint,Vba,Powerpoint,当用户在PPT幻灯片上运行GUI时,会显示一个userform,如下图所示 他们最多可以在复选框中选择3种危险。我试图弄清楚如何编写代码,使标记中数字最低的复选框进入“Hazard1”,数字第二低的标记(最多可选择3个)进入Hazard2,数字第三低的标记进入Hazard3。任何复选框的每个标记属性中只有一个数字。这是我将用于排名优先级的内容 以下是迄今为止的代码: Private Sub Hazards() Call Dictionary.HazardsDict 'Refer

当用户在PPT幻灯片上运行GUI时,会显示一个userform,如下图所示

他们最多可以在复选框中选择3种危险。我试图弄清楚如何编写代码,使标记中数字最低的复选框进入“Hazard1”,数字第二低的标记(最多可选择3个)进入Hazard2,数字第三低的标记进入Hazard3。任何复选框的每个标记属性中只有一个数字。这是我将用于排名优先级的内容

以下是迄今为止的代码:

Private Sub Hazards()
    Call Dictionary.HazardsDict

   'References the Dictionary for the Hazard Image options.

    Dim chkboxes As Variant
    Dim iCtrl As Long

    Select Case CountSelectedCheckBoxes(chkboxes)
        Case Is > 3
            MsgBox "Too many selected checkboxes!" & vbCrLf & vbCrLf & "please select three checkboxes only!", vbCritical
        Case Is = 1 'If only one checkbox is selected
            For iCtrl = LBound(chkboxes) To UBound(chkboxes)
              HazardList = Array(chkboxes(iCtrl).Caption)
              Debug.Print chkboxes(iCtrl).Caption
              Next
                'MsgBox chkboxes(iCtrl).Tag '<--| here you output each selected checkbox Tag. This is the "number" to use in the ranking
            For Each Ky In HazardList
                ActiveWindow.Selection.SlideRange.Shapes("Hazard1").Fill.UserPicture (dict5.Item(Ky)(0))
                ActiveWindow.Selection.SlideRange.Shapes("Hazard1Text").TextFrame.TextRange.Text = dict5.Item(Ky)(1)
            Next
        Case Is = 2 'If exactly 2 checkboxes are selected
            For iCtrl = LBound(chkboxes) To UBound(chkboxes)
              HazardList = Array(chkboxes(iCtrl).Caption)
              Debug.Print chkboxes(iCtrl).Caption
            Next
            For Each Ky In HazardList
                ActiveWindow.Selection.SlideRange.Shapes("Hazard1").Fill.UserPicture (dict5.Item(Ky)(0)) 'The checkbox with the lowest number in its Tag would go here.
                ActiveWindow.Selection.SlideRange.Shapes("Hazard1Text").TextFrame.TextRange.Text = dict5.Item(Ky)(1)
                ActiveWindow.Selection.SlideRange.Shapes("Hazard2").Fill.UserPicture (dict5.Item(Ky)(0)) 'The checkbox with the second lowest tag number would go here
                ActiveWindow.Selection.SlideRange.Shapes("Hazard2Text").TextFrame.TextRange.Text = dict5.Item(Ky)(1)
            Next
    End Select

    Set dict5 = Nothing

End Sub
Private Sub Hazards()
调用Dictionary.HazardsDict
'参考字典中的危险图像选项。
Dim chkboxes作为变体
暗iCtrl一样长
选择案例计数已选择复选框(chkboxes)
病例>3例
MsgBox“选择的复选框太多!”&vbCrLf&vbCrLf&“请仅选择三个复选框!”,vbCritical
如果只选中一个复选框,则大小写为=1
对于iCtrl=LBound(chkboxes)到UBound(chkboxes)
HazardList=数组(chkboxes(iCtrl).标题)
调试。打印chkboxes(iCtrl)。标题
下一个

“MsgBox chkboxes(iCtrl).Tag”您可以通过重新调整
CountSelectedCheckBoxes
函数返回包含所选复选框和计数的结构来完成此操作。下面是未经测试的代码,但我认为它会让您了解如何继续

声明自定义类型(结构)

返回所选复选框及其计数的函数

Public Type structChkBoxeses
    selectedCount   As Long
    first           As CheckBox
    second          As CheckBox
    third           As CheckBox
End Type
请注意,您需要修复正确排列复选框的逻辑

Function GetSelectedChkBoxes(structChkBoxes As structChkBoxeses) As structChkBoxeses

    Dim ctrl As Control


    ReDim chkboxes(1 To Me.Controls.Count)

    For Each ctrl In Me.Controls '<--| loop through userform controls
        If TypeName(ctrl) = "CheckBox" Then '<--| check if current control is a "checkbox" one
            If ctrl Then '<--| check if it's "checked"

                With structChkBoxes
                    .selectedCount = .selectedCount + 1 '<--| update checked checkboxes counter

                    ' you need to add some logic here but
                    ' basically you want get all the checkboxes
                    ' assigned to the right variables

                    If .third Is Nothing Then
                        Set .third = ctrl
                    Else
                        If .third.Tag > ctrl.Tag Then
                            Set .second = .third
                            Set .third = ctrl
                        End If
                    End If
                End With

            End If
        End If
    Next

    GetSelectedChkBoxes = structChkBoxes

End Function
可能是
structChkBoxes.first
需要类似于
CInt(structChkBoxes.first.tag)
。我不完全清楚你用什么作为字典的键


您还需要更改一些其他代码,因为您计算复选框的函数现在返回的是结构而不是数组。

能否显示
countselectedcheckbox
的代码,以便我们了解复选框对象?抱歉,我想包括这一点!我已经更新了帖子。我想你需要上传一个文件-这里有太多的事情需要我们去解决。好吧,太棒了。我完全明白你的意图。我明天可以测试这个方法,我会给你回复的。谢谢
Function GetSelectedChkBoxes(structChkBoxes As structChkBoxeses) As structChkBoxeses

    Dim ctrl As Control


    ReDim chkboxes(1 To Me.Controls.Count)

    For Each ctrl In Me.Controls '<--| loop through userform controls
        If TypeName(ctrl) = "CheckBox" Then '<--| check if current control is a "checkbox" one
            If ctrl Then '<--| check if it's "checked"

                With structChkBoxes
                    .selectedCount = .selectedCount + 1 '<--| update checked checkboxes counter

                    ' you need to add some logic here but
                    ' basically you want get all the checkboxes
                    ' assigned to the right variables

                    If .third Is Nothing Then
                        Set .third = ctrl
                    Else
                        If .third.Tag > ctrl.Tag Then
                            Set .second = .third
                            Set .third = ctrl
                        End If
                    End If
                End With

            End If
        End If
    Next

    GetSelectedChkBoxes = structChkBoxes

End Function
Sub Hazards(structChkBoxes As structChkBoxeses)

    For Each Ky In HazardList
        With ActiveWindow.Selection.SlideRange
            .Shapes("Hazard1").Fill.UserPicture (dict5.Item(structChkBoxes.first)(0))  'The checkbox with the lowest number in its Tag would go here.
            .Shapes("Hazard1Text").TextFrame.TextRange.Text = dict5.Item(Ky)(1)
            .Shapes("Hazard2").Fill.UserPicture (dict5.Item(structChkBoxes.second)(0)) 'The checkbox with the second lowest tag number would go here
            .Shapes("Hazard2Text").TextFrame.TextRange.Text = dict5.Item(Ky)(1)
        End With
    Next

End Sub