【问题标题】:Remove Item from Dropdown Box if Selected in Another Dropdown Box in VBA如果在 VBA 的另一个下拉框中选择,则从下拉框中删除项目
【发布时间】:2017-04-29 09:24:56
【问题描述】:

我在 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 下拉列表中选择的内容。

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

感谢您查看此内容!

【问题讨论】:

  • 我不完全清楚你在问什么。您是否希望 DropDown2 中的列表取决于 DropDown1 中选择的内容(如果有的话)? ...对于其他 3 个 DropDowns 也是如此。如果是,那么您的评论“因为有很多选择,所以不可能”可以解决,这可能是前进的方向......更多信息会有所帮助
  • 嗨。是的。如果在 DropDown1 或 DropDown2 等中选择了某些内容,那么在那里选择的任何内容都不会作为可能的选项显示在任何其他下拉列表中。这有意义吗?
  • 您的问题我仍然不清楚。试着换个说法。
  • 好的,在所有 5 个下拉菜单中,最初都有相同的选项 (1,2,3,4,5)。如果用户在 DropDown1 中选择了 1,那么该选项将不会出现在其他 4 个 DropDown 选项中。如果用户在 DropDown2 中选择了 5,那么该选项将不会出现在其他 4 个下拉菜单中。
  • 我一直在尝试这个,但是您的描述意味着当所有选项都被选中后,就无法更改任何内容,因为每个选项都必须排除已经为所有其他选项做出的选择 - 所以使用 5 个下拉菜单来选择 5 个选项表示 5 个下拉列表中的每一个都有 4 个排除值,因此每个下拉列表只有 1 个可行选项(最初选择后)。 5 个下拉菜单至少需要 6 个选项才能避免这种情况。

标签: vba powerpoint


【解决方案1】:
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

【讨论】:

  • 当。那很完美!正是我需要的。刚刚对其进行了修改以满足我的需求,并且效果很好。非常感谢!
猜你喜欢
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 2017-12-05
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
相关资源
最近更新 更多