Excel 我希望每次选择combobox值时都运行一个子例程。我该怎么做?
我有一个excel文件,有三列。 每一行都是唯一的。 这三列对应三个组合框 当选择第一个组合框中的选项时,我希望第二个组合框中填充与第一个组合框中的选择相对应的第二列中的项目。 即:Excel 我希望每次选择combobox值时都运行一个子例程。我该怎么做?,excel,combobox,vba,Excel,Combobox,Vba,我有一个excel文件,有三列。 每一行都是唯一的。 这三列对应三个组合框 当选择第一个组合框中的选项时,我希望第二个组合框中填充与第一个组合框中的选择相对应的第二列中的项目。 即: -亚当|苹果|种子 -Adam|Apple|Core -Adam|Orange|Peel -Jess|香蕉皮 -Jess |芒果|果肉 -Jess|Orange|Seed 这里第一列有数据Adam,Jess。第一个组合框已经有两个独特的选项Adam和Jess 一旦用户选择了Adam或Jess,第二个组合框应填充相应
-亚当|苹果|种子
-Adam|Apple|Core
-Adam|Orange|Peel
-Jess|香蕉皮
-Jess |芒果|果肉
-Jess|Orange|Seed
这里第一列有数据Adam,Jess。第一个组合框已经有两个独特的选项Adam和Jess
一旦用户选择了Adam或Jess,第二个组合框应填充相应的水果名称
例如,Combobox1=Adam
然后combobox2={苹果,橙色}
如果选择了Apple/Orange,则第三个组合框应具有与前两个组合框对应的选项
请注意,我的第一个组合框已正确填充数据:
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
编辑:我基本上需要这样说:
用第二行的单元格填充第二个combobox,其中第一行等于combobox 1。假设您的数据在sheet1中: 1-您可以使用combobox\u Change事件处理程序跟踪用户何时从combobox中选择了新值 2-Combobox.listindex:这将为您提供Combobox中当前选定的索引 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
Private子组合框1_Change()
作为字符串的Dim strValue
作为整数的Dim i
如果ComboBox1.ListIndex为-1,则
strValue=ComboBox1.List(ComboBox1.ListIndex)
组合框2.清除
组合框3.清除
对于i=1到6
如果单元格(i,1)=标准值,则
如果存在(ComboBox2,单元格(i,2))=False,则
ComboBox2.AddItem(单元格(i,2))
如果结束
如果存在(ComboBox3,单元格(i,3))=False,则
ComboBox3.AddItem(单元格(i,3))
如果结束
如果结束
接下来我
其他的
组合框2.清除
组合框3.清除
如果结束
端接头
专用子组合框2_Change()
作为字符串的Dim strValue1
作为字符串的Dim strValue2
作为整数的Dim i
如果是(ComboBox2.ListIndex-1)和(ComboBox1.ListIndex-1),则
strValue1=ComboBox2.List(ComboBox2.ListIndex)
strValue2=ComboBox1.List(ComboBox1.ListIndex)
组合框3.清除
对于i=1到6
如果(单元格(i,2)=标准值1)和(单元格(i,1)=标准值2),则
如果存在(ComboBox3,单元格(i,3))=False,则
ComboBox3.AddItem(单元格(i,3))
如果结束
如果结束
接下来我
其他的
组合框3.清除
如果结束
端接头
私有子用户表单_初始化()
作为整数的Dim i
对于i=1到6
如果存在(ComboBox1,单元格(i,1))=False,则
ComboBox1.AddItem(单元格(i,1))
如果结束
接下来我
端接头
私有函数以布尔形式存在(ByRef objCmb作为组合框,ByVal strValue作为字符串)
作为整数的Dim i
对于i=1到objCmb.ListCount
如果strValue=objCmb.List(i-1),那么
存在=真
退出功能
如果结束
接下来我
存在=错误
端函数
假设您有三个组合框,名称分别为Combobox1、Combobox2和Combobox3(VBA编辑器创建的默认名称)为什么不直接使用
ComboBox\u Change
事件?@chrisneilsen我在网上找到了这个选项。这会有帮助吗?这是针对.Net的,不是VBA,所以不。名称正确吗?组合框1、组合框2和组合框3?