如果在VBA中的另一个下拉框中选中,则从下拉框中删除项目

时间:2017-04-28 16:02:02

标签: vba powerpoint

我在Powerpoint中有一个用户表单,其上有5个页面(顶部的标签,您点击的每个标签都会将您带到同一个用户表单中的新页面)。我在每个页面上都有一个组合框下拉列表。它们都是从同一位置填充的(vba代码中的模块)。以下是存储下拉选项列表的代码:

Sub HazardTypeSevere()

'List of items to include in the Severe dropdown.

Dim strlist As String
     'Put them in an Array declared as Public
     strlist = "Damaging Winds,Large Hail,Tornadoes"
     rayNames = Split(strlist, ",")

End Sub

在我的用户表单代码中,这是填充下拉列表的代码:

Private Sub HazardType2_DropButtonClick()
Call DropdownOptions.HazardTypeSevere

'List all the severe hazard type dropdown options in Hazard 2


'This is where I am trying to tell it to remove whatever is selected in the 
'HazardType1 dropdown from the HazardType2 dropdown.
If HazardType1 <> "" Then 'If anything is selected in the HazardType1 
'dropdown...
HazardType2.RemoveItem (HazardType1.Value) 'Remove the item that is 
'selected in HazardType1
End If

Dim L As Long

    If HazardType2.ListCount = 0 Then
        With HazardType2
        .Clear
        For L = 0 To UBound(rayNames)
            .AddItem rayNames(L)
        Next L
        .Value = rayNames(0)
        End With
    End If

End Sub

当我运行上面的代码时,我收到“无效参数”错误。 'HazardType1.Value'确实显示了在HazardType1下拉列表中选择的内容。

我不确定这是否可行,如果没有单独的项目列表来调用以填充下拉列表。但这不可能,因为下拉框中有很多选项。

谢谢你看看这个!

1 个答案:

答案 0 :(得分:0)

Private Sub UserForm_Initialize()
 ComboBox1.List = Split(" ,1,2,3,4,5", ",")
 ComboBox2.List = Split(" ,1,2,3,4,5", ",")
 ComboBox3.List = Split(" ,1,2,3,4,5", ",")
 ComboBox4.List = Split(" ,1,2,3,4,5", ",")
 ComboBox5.List = Split(" ,1,2,3,4,5", ",")
End Sub

Private Sub ComboBox1_Change()
   ManageDropDowns (1)
End Sub

Private Sub ComboBox2_Change()
   ManageDropDowns (2)
End Sub

Private Sub ComboBox3_Change()
   ManageDropDowns (3)
End Sub

Private Sub ComboBox4_Change()
   ManageDropDowns (4)
End Sub

Private Sub ComboBox5_Change()
   ManageDropDowns (5)
End Sub

Sub ManageDropDowns(IDNum As Integer)
   '
   ' Presuming we have 5 Comboboxes
   ' This routine ensures there are initially 6 choices for each ComboBox - a blank empty choice and 5 values 1-5
   ' This will then ensure no 2 ComboBoxes can make the same choice from 1-5 (all can choose blank)
   '
   Dim UsedList As String, FullList As String, Left2Use As String
   Dim FullArray As Variant, UsedArray(1 To 5) As String

   UL1 = ""
   UL1 = UL1 & IIf(Trim(ComboBox2.Value) = "", "", "" & Trim(ComboBox2.Value) & ",")
   UL1 = UL1 & IIf(Trim(ComboBox3.Value) = "", "", "" & Trim(ComboBox3.Value) & ",")
   UL1 = UL1 & IIf(Trim(ComboBox4.Value) = "", "", "" & Trim(ComboBox4.Value) & ",")
   UL1 = UL1 & IIf(Trim(ComboBox5.Value) = "", "", "" & Trim(ComboBox5.Value) & ",")
   While Right(UL1, 1) = ","
      UL1 = Left(UL1, Len(UL1) - 1)
   Wend

   UL2 = ""
   UL2 = UL2 & IIf(Trim(ComboBox1.Value) = "", "", "" & Trim(ComboBox1.Value) & ",")
   UL2 = UL2 & IIf(Trim(ComboBox3.Value) = "", "", "" & Trim(ComboBox3.Value) & ",")
   UL2 = UL2 & IIf(Trim(ComboBox4.Value) = "", "", "" & Trim(ComboBox4.Value) & ",")
   UL2 = UL2 & IIf(Trim(ComboBox5.Value) = "", "", "" & Trim(ComboBox5.Value) & ",")

   UL3 = ""
   UL3 = UL3 & IIf(Trim(ComboBox2.Value) = "", "", "" & Trim(ComboBox2.Value) & ",")
   UL3 = UL3 & IIf(Trim(ComboBox1.Value) = "", "", "" & Trim(ComboBox1.Value) & ",")
   UL3 = UL3 & IIf(Trim(ComboBox4.Value) = "", "", "" & Trim(ComboBox4.Value) & ",")
   UL3 = UL3 & IIf(Trim(ComboBox5.Value) = "", "", "" & Trim(ComboBox5.Value) & ",")

   UL4 = ""
   UL4 = UL4 & IIf(Trim(ComboBox2.Value) = "", "", "" & Trim(ComboBox2.Value) & ",")
   UL4 = UL4 & IIf(Trim(ComboBox3.Value) = "", "", "" & Trim(ComboBox3.Value) & ",")
   UL4 = UL4 & IIf(Trim(ComboBox1.Value) = "", "", "" & Trim(ComboBox1.Value) & ",")
   UL4 = UL4 & IIf(Trim(ComboBox5.Value) = "", "", "" & Trim(ComboBox5.Value) & ",")

   UL5 = ""
   UL5 = UL5 & IIf(Trim(ComboBox2.Value) = "", "", "" & Trim(ComboBox2.Value) & ",")
   UL5 = UL5 & IIf(Trim(ComboBox3.Value) = "", "", "" & Trim(ComboBox3.Value) & ",")
   UL5 = UL5 & IIf(Trim(ComboBox4.Value) = "", "", "" & Trim(ComboBox4.Value) & ",")
   UL5 = UL5 & IIf(Trim(ComboBox1.Value) = "", "", "" & Trim(ComboBox1.Value) & ",")

   FullList = "1,2,3,4,5"
   FullArray = Split(FullList, ",")
   Left2Use = " ,"

   If IDNum <> 1 Then
      UsedList = UL1
      Left2Use = " "  ' Space
      For i = 0 To UBound(FullArray)
         iTxt = Trim("" & i + 1)
         If Not (InStr(1, UsedList, iTxt) > 0) Then
            ' Not Already Used - Add it to Left2Use
            Left2Use = Left2Use & "," & iTxt
         End If
      Next i
      ComboBox1.List = Split(Left2Use, ",")
   End If
   If IDNum <> 2 Then
      UsedList = UL2
      Left2Use = " "  ' Space
      For i = 0 To UBound(FullArray)
         iTxt = Trim("" & i + 1)
         If Not (InStr(1, UsedList, iTxt) > 0) Then
            ' Not Already Used - Add it to Left2Use
            Left2Use = Left2Use & "," & iTxt
         End If
      Next i
      ComboBox2.List = Split(Left2Use, ",")
   End If
   If IDNum <> 3 Then
      UsedList = UL3
      Left2Use = " "  ' Space
      For i = 0 To UBound(FullArray)
         iTxt = Trim("" & i + 1)
         If Not (InStr(1, UsedList, iTxt) > 0) Then
            ' Not Already Used - Add it to Left2Use
            Left2Use = Left2Use & "," & iTxt
         End If
      Next i
      ComboBox3.List = Split(Left2Use, ",")
   End If
   If IDNum <> 4 Then
      UsedList = UL4
      Left2Use = " "  ' Space
      For i = 0 To UBound(FullArray)
         iTxt = Trim("" & i + 1)
         If Not (InStr(1, UsedList, iTxt) > 0) Then
            ' Not Already Used - Add it to Left2Use
            Left2Use = Left2Use & "," & iTxt
         End If
      Next i
      ComboBox4.List = Split(Left2Use, ",")
   End If
   If IDNum <> 5 Then
      UsedList = UL5
      Left2Use = " "  ' Space
      For i = 0 To UBound(FullArray)
         iTxt = Trim("" & i + 1)
         If Not (InStr(1, UsedList, iTxt) > 0) Then
            ' Not Already Used - Add it to Left2Use
            Left2Use = Left2Use & "," & iTxt
         End If
      Next i
      ComboBox5.List = Split(Left2Use, ",")
   End If
End Sub