ユーザーがPPTスライド上でGUIを実行すると、以下に示すユーザーフォームが表示されます。PowerPoint VBAで配列の中で最も低い値、2番目に低い値、最も高い値を見つける
彼らは、チェックボックスで3つの危険まで選択することができます。私は、そのタグの中で最も小さい数字のチェックボックスが "Hazard1"になるようにコードを書く方法を見つけようとしています。2番目に小さい番号のタグ(選択できる3つまで)がHazard2に入ります3番目に小さい番号のタグがHazard3に入ります。任意のチェックボックスの各Tagプロパティには、単一の数字だけが含まれています。これは私がランキングの優先順位に使うものです。ここで
は、これまでのコードです:
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
次のように上記のコードが参照する辞書は次のとおりです。
Public dict, dict2, dict3, dict4, dict5 As Object, Key, val 'Makes the dictionaries public so they can be accessed by other Modules.
Sub MainImageDict()
'This is the dictionary for the Main Image portion of the slides.
Set dict3 = CreateObject("Scripting.Dictionary")
Key = "Day 1 High Temperatures": val = Array("URL_to_Image")
dict3.Add Key, val
Key = "Day 2 High Temperatures": val = Array("URL_to_Image")
dict3.Add Key, val
End Sub
Function CountSelectedCheckBoxes(chkboxes As Variant) As Long
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"
CountSelectedCheckBoxes = CountSelectedCheckBoxes + 1 '<--| update checked checkboxes counter
Set chkboxes(CountSelectedCheckBoxes) = ctrl '<--| store it in the array
End If
End If
Next
If CountSelectedCheckBoxes > 0 Then ReDim Preserve chkboxes(1 To CountSelectedCheckBoxes) '<--|size checkboxes array to actual checked checkboxes found
End Function
任意のアイデアは?ありがとうございました!
「CountSelectedCheckBoxes」のコードを表示して、チェックボックスオブジェクトの意味を知ることができますか? –
申し訳ありませんが、私はそれを含めることを意味しました!私は投稿を更新しました。 – hunter21188
私はファイルをアップロードする必要があると思います。私たちが解決するにはあまりにも多くのことがあります。 – brettdj