我如何使用OptionBox来驱动组合框的值与我的工作表中的选择性值?

问题描述 投票:0回答:1

我正在寻找一种方法,可以很容易地根据所选的选项框在组合框列表中链接工作表中的选择值。我是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

这是在不包含上面最后6行代码的情况下,选择选项框时的表单结果。

excel vba subroutine
1个回答
0
投票

根据你的用户表单图像,我已经复制了你的表单与 UserForm1 以及所有控件的所有默认名称。

如果您移动您的 If...Then 向您的 Optionbutton_Change() 事件,您将填充您的列表。

关于微软的文档 Initialize 事件:

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 因为选项按钮在其组中任何时候只能选择一个。
  • 我在一个与你的工作表无关的范围内使用了虚拟数据--但你只需要改变对你的工作表和范围的引用。
  • 我已经包含了 Me.ComboBox1.Clear 作为第一条执行的语句,如果 OptionButton 因为这将清除可能已经被填满的列表。
    • 如果您省略了这一点,而用户点击了 OptionButton1 填充组合框中的第一个范围的值,然后他们点击 OptionButton2 你的第二个范围的值会被添加到列表的底部,在已经填充的第一个范围的值之后。
  • 我已经在 ComboBox 加上 Initialize 事件。这不是强制性的。

表单行为的截图示例。

Sheet1上的虚拟数据:

Dummy data on sheet1

第一次打开。

UserForm first opened.

OptionButton1列表:_____________________ OptionButton2列表: 选项按钮2列表:_____________________ OptionButton2 list: _______________________

Option button 1 list Option button 2 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
© www.soinside.com 2019 - 2024. All rights reserved.