我正在寻找一种方法,可以很容易地根据所选的选项框在组合框列表中链接工作表中的选择值。我是VBA新手,我想知道我的语法是否有误。我认为我的方法是正确的,但当选项被选中时,我一直得到一个错误。Compile Error: Sub of Function not defined. 这个错误存在于Private Sub选项按钮运行时。如果我删除这部分代码,那么我的ComboBox列表是空的,但我没有得到任何错误信息...我习惯于用JavaScript工作,所以调用函数是很常见的,也许用VBA不是这样?
Dim myTable As Range
'Updated by Extendoffice 2018/1/30
Private Sub UserForm_Initialize()
Set myTable = Worksheets("Sheet1").Range("A2:B8")
If OptionButton1 = True And OptionButton2 = False Then
Me.ComboBox1.List = myTable.Range("A2:A3").Value
ElseIf OptionButton2 = True And OptionButton1 = False Then
Me.ComboBox1.List = myTable.Range("A5:A8").Value
End If
End Sub
Private Sub OptionButton1_Change()
Call UserForm
End Sub
Private Sub OptionButton2_Change()
Call UserForm
End Sub
根据你的用户表单图像,我已经复制了你的表单与 UserForm1
以及所有控件的所有默认名称。
如果您移动您的 If...Then
向您的 Optionbutton_Change()
事件,您将填充您的列表。
Initialize事件通常用于准备一个应用程序或UserForm的使用.变量被分配初始值,控件可以移动或调整大小以适应初始化数据。
所以不是为了更新数据,一旦你已经加载了表单。
我在一个新的工作簿上测试了以下内容,并在其中加入了虚拟数据。A1:A10
第一个选项按钮列表和 B1:B10
为第二个选项按钮列表。
注意: 这些都包含在 UserForm
代码模块。
Option Explicit
Private Sub OptionButton1_Change()
Dim TargetRange As Range
Dim TargetCell As Range
If Me.OptionButton1 = True Then
Me.ComboBox1.Clear '<~~ This removes the previous list items from the combobox.
Me.ComboBox1.Value = "" '<~~ Removes the default value added at initialization.
Set TargetRange = Sheet1.Range("A1:A10")
For Each TargetCell In TargetRange
Me.ComboBox1.AddItem TargetCell.Value
Next TargetCell
End If
End Sub
Private Sub OptionButton2_Change()
If Me.OptionButton2 = True Then
Me.ComboBox1.Clear '<~~ This removes the previous list items from the combobox.
Me.ComboBox1.Value = "" '<~~ Removes the default value added at initialization.
Set TargetRange = Sheet1.Range("B1:B10")
For Each TargetCell In TargetRange
Me.ComboBox1.AddItem TargetCell.Value
Next TargetCell
End If
End Sub
Private Sub UserForm_Initialize()
Me.ComboBox1.Value = "Select an OptionButton below..."
End Sub
ComboBox
列入 OptionButton_Change()
事件处理程序。True
另一个是 False
因为选项按钮在其组中任何时候只能选择一个。 Frame
它们会自动与选项按钮分开归类在 Frame
(此处指出,两者互为表里). Me.ComboBox1.Clear
作为第一条执行的语句,如果 OptionButton
因为这将清除可能已经被填满的列表。 OptionButton1
填充组合框中的第一个范围的值,然后他们点击 OptionButton2
你的第二个范围的值会被添加到列表的底部,在已经填充的第一个范围的值之后。ComboBox
加上 Initialize
事件。这不是强制性的。Sheet1上的虚拟数据:
第一次打开。
OptionButton1列表:_____________________ OptionButton2列表: 选项按钮2列表:_____________________ OptionButton2 list: _______________________
你可以把整个事情写成一个函数(或子程序),然后调用 该 在你试图调用的方式。Initialize
事件。
下面是一个子程序的例子。
Public Sub ChangeMyComboBoxList()
Dim TargetRange As Range
Dim TargetCell As Range
If UserForm1.OptionButton1 = True Then
UserForm1.ComboBox1.Clear '<~~ This removes the previous list items from the combobox.
UserForm1.ComboBox1.Value = "" '<~~ Removes the default value added at initialization.
Set TargetRange = Sheet1.Range("A1:A10")
For Each TargetCell In TargetRange
Me.ComboBox1.AddItem TargetCell.Value
Next TargetCell
ElseIf UserForm1.OptionButton2 = True Then
UserForm1.ComboBox1.Clear '<~~ This removes the previous list items from the combobox.
UserForm1.ComboBox1.Value = "" '<~~ Removes the default value added at initialization.
Set TargetRange = Sheet1.Range("B1:B10")
For Each TargetCell In TargetRange
UserForm1.ComboBox1.AddItem TargetCell.Value
Next TargetCell
End If
End Sub
Private Sub OptionButton1_Change()
ChangeMyComboBoxList '<~~ Call is not required
End Sub
Private Sub OptionButton2_Change()
ChangeMyComboBoxList '<~~ Call is not required
End Sub