2017-03-05 4 views
0

ユーザーがPPTスライドでGUIを実行すると、以下のようなユーザーフォームが表示されます。 The userform that pops up when runVBoxを使用したPowerpointのユーザーフォームのチェックボックスループ

これらのチェックボックスでは、最大3つのハザードを選択できます。どのチェックボックスが選択されているかを確認するために、すべてのチェックボックスをループする方法を見つけようとしています(どれも選択されていない場合は、サブを終了します)。次に、どちらが選択されているかに基づいて、対応するイメージをPPTスライド内の図形に入力します。形状は左から右に並んでいます。 「最も高いランク付け」の選択は左の形になり、3つの「最も低いランク付け」が右側のボックスに入ります。オプションが2つしか選択されていない場合、第1および第2の形状のみが充填される。私は、各オプションの重要度の順序に値(1,2,3など)を割り当てる最も簡単な方法がわかりません。私は、If - > Thenステートメントですべての組み合わせをカバーする必要がない方が好きです。というのも、面倒で時間がかかるからです。私はこれを行うより良い方法があると思いますか?

私は次のコードを使用して簡単にコンボボックスのリストをループできる午前:

Private Sub MainImage() 
Call Dictionary.MainImageDict 

'References the Dictionary for the Main Image options. 

ComboBoxList = Array(CStr(ComboBox2)) 'ComboBox2 is the Main Image dropdown. 

    For Each Ky In ComboBoxList 
    On Error Resume Next 
    'If nothing is selected in the Main Image dropdown, do nothing and exit this sub. 
    If Ky = "" Then 
    Exit Sub 
    'Otherwise, if anything is selected in the Main Image dropdown, insert image and remove placeholder text. 
    ElseIf Ky <> "" Then 
    ActiveWindow.Selection.SlideRange.Shapes("MainImage").Fill.UserPicture (dict3.Item(Ky)(0)) 

    End If 

    Next 

Set dict3 = 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 

私は似た何かをしたいと思い、可能であれば、チェックボックスをオンにします。彼らはすべて別々であり、コンボボックスのように1つに結合されているわけではないので、私はこれをどうやって行うのかについてはわかりません。どんな助けでも大歓迎です!私が言ったことが意味をなさないと言ったら教えてください。ありがとうございました!

提案コードを使用して更新されたコードは

素晴らしく動作します。今私は1つの最終的な質問があります。

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. you can use this "number" 
      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 

私はそのタグで最も小さい番号のチェックボックスが第二に、「Hazard1」、にタグを行くように、私はコードを書くだろうか把握しようとしています:私は、次のようにコードを変更しました最も低い番号(選択可能な3つまで)がHazard2に入り、3番目に小さい番号のタグがHazard3に入ります。何か案は?ありがとうございました!

答えて

1

ユーザーフォームControlsコレクションを反復処理し、VBA IDEプレスF4でプロパティをポップするとき、あなたのチェックボックスコントロールのTagプロパティにいくつかの「重要度評価」(格納することができながら、「チェックボックス」TypeName

とのそれらのために確認することができます窓、ユーザーフォーム内の各チェックボックスを選択し、Tagエントリに適切な「重要性の評価」と入力)

次のように利用することができます上記のものをすべて:

Private Sub InsertBtn_Click() 
    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 < 3 
      MsgBox "Too few selected checkboxes!" & vbCrLf & vbCrLf & "please select three checkboxes!", vbCritical    
     Case Else 
      For iCtrl = LBound(chkboxes) To UBound(chkboxes) 
       MsgBox chkboxes(iCtrl).Tag '<--| here you output each selected checkbox Tag. you can use this "number" 
      Next 
    End Select 
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 
+0

それは完全に動作します。ありがとうございました!私は最後の質問が1つあります。私は元の質問を修正しました。あなたは修正されたコードと私の質問を下に向かって見ることができます。再度、感謝します! – hunter21188

+1

ようこそ。解決策を受け取った後で質問を変更すると、ここで許可されていない "シャンレオン"の質問につながることにご注意ください。だから、私の答えがあなたの_original_質問を解決したら、それを受け入れたものとしてマークしてください。 _new_号に_new_投稿を作成したい場合があります。ありがとうございました – user3598756

関連する問題