vba 从用户表单组合框中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19776105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:15:15  来源:igfitidea点击:

Removing items from a userform combobox

excelvbacomboboxuserform

提问by CBC_NS

I have a couple of comboboxes which all have the same values - "", 1-12. I'm trying to get it so that when a user selects an item, all of the other items disappear. When a new item is selected, it repopulates the last item.

我有几个组合框,它们都具有相同的值 - "", 1-12。我试图得到它,以便当用户选择一个项目时,所有其他项目都消失了。When a new item is selected, it repopulates the last item.

This is what I have that far.

这就是我所拥有的。

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

My first run through, it will remove the first item. Let's say for example I click 1. It will remove (1). If I click 2 it will remove (3). I believe it does this because it is removing it by index value. 1 => "", (1). Thus the array is "", 2, 3, etc. 2 => (3). I'm sorry if that was poorly demoed. Anyways, how can I get it so that it will remove the value from the array, and reset the array, or how can I remove a value from the array where it matches the value and not the index equaling the index?

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

Combobox is being populated via a range on my excel sheet

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

回答by CBC_NS

The solution I found for my question is as follows:

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

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

This applies to each combobox with numbers increasing accordingly. This solution works best in this scenario considering that the values are numeric to begin with and are in order.

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