[VBA组合框按条件列表

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

我正在尝试使用Userform ComboBox创建列表。该列表将从电子表格中获取数据,可以将其更新并推回到电子表格中。但是,在按条件创建列表时,我迷失了。

我有Range(A:A),它具有任务标题。在范围(D:D)中,我具有任务的状态(进行中,用于检查,已批准和已发布)。我要创建的列表是列出任务标题,其中D =用于检查。

[能否请您指出正确的方向,以便能够在VBA中编写此内容。

excel vba combobox userform
1个回答
0
投票

例如,您可以将以下代码添加到用户表单中

Option Explicit


Private Sub UserForm_Initialize()

    ' I assume the list is on the Activesheet
    ' and it has a header row
    Dim rg As Range
    Set rg = Range("A1").CurrentRegion
    Set rg = rg.Offset(1).Resize(rg.Rows.Count - 1)

    Dim vDat As Variant

    ' Goto Tools/References and check Microsoft Scripting Runtime
    Dim rDict As Scripting.Dictionary
    Set rDict = New Scripting.Dictionary

    vDat = rg.Value2

    Dim i As Long
    For i = LBound(vDat) To UBound(vDat)
        ' If column D contains "For Check"
        ' add the task from column A to the dictionary
        If vDat(i, 4) = "For Check" Then
            rDict(vDat(i, 1)) = vDat(i, 1)
        End If
    Next i

    ' I assume the name of the combobox is combobox1
    ComboBox1.List = rDict.Keys
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.