【问题标题】:Removing items from a userform combobox从用户窗体组合框中删除项目
【发布时间】:2013-11-05 21:30:56
【问题描述】:

我有几个组合框,它们都具有相同的值 - “”,1-12。我试图得到它,以便当用户选择一个项目时,所有其他项目都会消失。当一个新项目被选中时,它会重新填充最后一个项目。

这就是我到目前为止所拥有的。

Private Old182 As String
Private Sub ComboBox182_Change()
Dim i
Dim y
If ComboBox182 <> Old182 Then
    For i = 182 To 193
        If i <> 182 Then
            If ComboBox182 <> Old182 Then
                If ComboBox182 <> "" Then
                    Controls("ComboBox" & i).RemoveItem ComboBox182.Value
                    If ComboBox182 <> "" Then
                        If Old182 <> "" Then
                            Controls("ComboBox" & i).AddItem Old182
                        End If
                    End If
                End If
            End If
        End If
    Next
End If
Old182 = ComboBox182.Value
End Sub

我的第一次运行,它将删除第一个项目。例如,假设我单击 1。它将删除 (1)。如果我单击 2,它将删除 (3)。我相信它会这样做,因为它是按索引值删除它。 1 =>“”,(1)。因此数组是“”、2、3等。2 => (3)。如果演示效果不佳,我很抱歉。无论如何,我怎样才能得到它,以便它从数组中删除值,并重置数组,或者我怎样才能从数组中删除与值匹配的值而不是等于索引的索引?

正在通过我的 Excel 工作表上的范围填充组合框

【问题讨论】:

  • 我看不到的是 .Clear method 在添加新元素之前您可能应该使用它。您拥有所需的一切 - 只需重新考虑您的逻辑即可。
  • 我正在想办法正确地包含您的建议。当我在 removeitem 行之后添加它时,它会按预期清除我的其余框,但是,我只想清除一项。
  • 您无法清除一项。您需要将它们全部清除并再次添加一组新的正确项目。

标签: excel vba combobox userform


【解决方案1】:

我为我的问题找到的解决方案如下:

Dim i
Dim y
Dim z As String
z = ""
If ComboBox182 <> Old182 Then
    For i = 182 To 193
        If i <> 182 Then
            If ComboBox182 <> Old182 Then
                If Old182 <> "" Then
                    Controls("ComboBox" & i).AddItem Old182, Old182
                    Controls("ComboBox" & i).RemoveItem (Old182 + 1)
                End If
                Controls("ComboBox" & i).RemoveItem ComboBox182.ListIndex
                Controls("ComboBox" & i).AddItem z, ComboBox182.ListIndex
            End If
        End If
    Next
End If
    Old182 = ComboBox182.Value

这适用于数字相应增加的每个组合框。考虑到值以数字开头且按顺序排列,此解决方案在这种情况下效果最佳。

【讨论】:

    猜你喜欢
    • 2023-02-17
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多