2017-03-09 73 views
0

當用戶在PPT幻燈片上運行GUI時,它會顯示一個用戶窗體,如下圖所示。在PowerPoint中查找數組中的最低值,次最低值和最高值VBA

enter image description here

他們最多可以選擇在複選框3個危害。我試圖弄清楚如何編寫代碼,以便標籤中編號最小的複選框進入「Hazard1」,具有次數最少的標籤(最多可選擇3個)進入Hazard2 ,第三低的標籤進入Hazard3。任何複選框的每個Tag屬性只有一個數字。這是我將用於我的排名優先。

這是迄今爲止代碼:

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 

任何想法:

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 

上面的代碼指的是如下的解釋?謝謝!

+0

你可以顯示「CountSelectedCheckBoxes」的代碼,以便我們可以瞭解複選框對象嗎? –

+0

對不起,我的意思是包括在內!我已更新該帖子。 – hunter21188

+0

我認爲你需要上傳一個文件 - 這裏有太多的東西要我們解決。 – brettdj

回答

0

您可以通過調整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 

如何利用結構來獲取正確的字典項

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 

可能structChkBoxes.first需要是這樣的CInt(structChkBoxes.first.tag)例。我不完全清楚你用什麼字典鍵。

您還需要更改一些其他代碼,因爲您計算複選框的函數現在正在返回一個結構而不是數組。

+0

好的,真棒。我完全明白你要去哪裏。我明天將能夠測試這種方法,我會盡快回復您。謝謝! – hunter21188