【问题标题】:I want a subroutine to run everytime a combobox value is selected. How do I do this?我希望每次选择组合框值时运行一个子例程。我该怎么做呢?
【发布时间】:2014-06-20 14:45:54
【问题描述】:

我有一个包含三列的 excel 文件。 每一行都是唯一的。 这三列对应三个组合框。

当第一个组合框中的选项被选中时,我希望第二个组合框填充第二列中与第一个组合框中的选择相对应的项目。 即:

- Adam | Apple | Seed

- Adam | Apple | Core

- Adam | Orange | Peel

- Jess | Banana | Peel

- Jess | Mango | Pulp

- Jess | Orange | Seed

这里的第一列有数据 Adam,Jess。第一个组合框已经有两个独特的选项 Adam 和 Jess。

一旦用户选择了 Adam 或 Jess,第二个组合框应填充相应的水果名称。

例如。 Combobox1 = 亚当

然后 combobox2 ={Apple, Orange}

when either Apple / Orange are selected then the third combobox should have options correspoding to the first two comboboxes.

请注意,我的第一个组合框已经正确填充了数据:

Function UniqueList()
'Populate control with
'unique list.

Range("Names").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Range("uniqueNames"), Unique:=True

'Set combo control's Row Source property.

Range("uniqueNames").RemoveDuplicates Columns:=1, Header:=xlNo
UserForm1.uniqueNameList.RowSource = Selection.CurrentRegion.Address

'Display user form.
UserForm1.Show

Selection.CurrentRegion.Clear

End Function

编辑:我基本上需要这样说: 用第一行等于 combobox1 的第二行的单元格填充第二个组合框。

【问题讨论】:

  • 为什么不直接使用ComboBox_Change 事件?
  • @chrisneilsen 我在网上找到了这个。 msdn.microsoft.com/en-us/library/…这会有帮助吗?
  • 那是 .Net 的,不是 VBA 的,所以没有。

标签: excel combobox vba


【解决方案1】:

假设您在 sheet1 中有数据:

1- 您可以使用 combobox_Change 事件处理程序来跟踪用户何时从组合框中选择了新值。

2- Combobox.listindex:这将为您提供组合框中当前选定的索引

3- Combobox.list:是一个包含组合框中所有项目的数组

 Private Sub ComboBox1_Change()
    Dim strValue As String
    Dim i As Integer
    If ComboBox1.ListIndex <> -1 Then
        strValue = ComboBox1.List(ComboBox1.ListIndex)
        ComboBox2.Clear
    ComboBox3.Clear
    For i = 1 To 6
        If Cells(i, 1) = strValue Then
            If exists(ComboBox2, Cells(i, 2)) = False Then
                ComboBox2.AddItem (Cells(i, 2))
            End If
            If exists(ComboBox3, Cells(i, 3)) = False Then
                ComboBox3.AddItem (Cells(i, 3))
            End If

        End If

    Next i
Else
    ComboBox2.Clear
    ComboBox3.Clear
End If

End Sub



Private Sub ComboBox2_Change()
Dim strValue1 As String
Dim strValue2 As String
Dim i As Integer
If (ComboBox2.ListIndex <> -1) And (ComboBox1.ListIndex <> -1) Then
    strValue1 = ComboBox2.List(ComboBox2.ListIndex)
    strValue2 = ComboBox1.List(ComboBox1.ListIndex)
    ComboBox3.Clear
    For i = 1 To 6
        If (Cells(i, 2) = strValue1) And (Cells(i, 1) = strValue2) Then
            If exists(ComboBox3, Cells(i, 3)) = False Then
                ComboBox3.AddItem (Cells(i, 3))
            End If    
        End If
    Next i
    Else
        ComboBox3.Clear  
    End If
End Sub

Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 6
    If exists(ComboBox1, Cells(i, 1)) = False Then
        ComboBox1.AddItem (Cells(i, 1))
    End If
Next i
End Sub

Private Function exists(ByRef objCmb As ComboBox, ByVal strValue As String) As Boolean
Dim i  As Integer

For i = 1 To objCmb.ListCount
    If strValue = objCmb.List(i - 1) Then
        exists = True
        Exit Function
    End If

Next i
exists = False
End Function

假设您有 3 个组合框,名称为 Combobox1、Combobox2 和 Combobox3(VBA 编辑器创建的默认名称),该代码将起作用

【讨论】:

  • 名称是否正确? Combobox1、Combobox2 和 Combobox3?
猜你喜欢
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多