2017-03-09 2 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 

CInt(structChkBoxes.first.tag) 같은 것으로 정확한 사전 항목을

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 요구를 가져 구조체를 사용하는 방법의 예. 사전 키로 사용하는 것이 무엇인지 완전히 명확하지 않습니다.

확인란을 계산하는 함수가 배열 대신 구조체를 반환하기 때문에 다른 코드도 변경해야합니다.

+0

좋습니다. 나는 너가 이것으로 어디로 가고 있는지 명확하게 본다. 나는이 방법을 내일 시험 할 수있을 것이고 나는 다시 너에게 갈 것이다. 감사! – hunter21188

관련 문제