【问题标题】:How to populate a listbox based on a conditional statement如何根据条件语句填充列表框
【发布时间】:2020-05-03 11:28:30
【问题描述】:

我正在尝试在用户窗体中填充列表框,但列表框需要根据活动工作表上单元格中的内容进行更改。复杂之处在于我试图引用另一张纸上的命名范围。因此,例如,如果单元格显示“hi” - 我想检查单元格是否显示 hi,然后转到另一个名为“hi”的工作表上的命名范围,并将该范围内的值带入列表框中。

这是我目前所拥有的:

Private Sub UserForm_Initialize()

'Populate Combobox Based on Cell Value
Dim celltxt As String
celltxt = ActiveSheet.Range("cellTest").Text

    If InStr(1, celltxt, "hi") Then
        'Code to bring in routes from named range called "hi"
        ListBox1.RowSource = Worksheets("Sheet4").Range("hi").Value
    End If

End Sub

我会根据其他单元格值(例如“嘿”或“发生什么事”)多次重复“if-end if”代码段。

我不断收到运行时 1004 错误。救命!!

【问题讨论】:

标签: excel vba listbox conditional-statements


【解决方案1】:

问题是我将“Sheet 4”重命名为“DropDown”,因为它是我存储所有下拉菜单的地方。代码应为:

Private Sub UserForm_Initialize()

'Populate Combobox Based on Cell Value
Dim celltxt As String
celltxt = ActiveSheet.Range("cellTest").Text

    If InStr(1, celltxt, "hi") Then
        'Code to bring in routes from named range called "hi"
        ListBox1.RowSource = Worksheets("DropDown").Range("hi").Value
    End If

End Sub

效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-04
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多