根据单元格值填充列表框

时间:2019-05-08 10:48:46

标签: excel vba

我正在基于

填充一个列表框
With lbInterviews
   .RowSource = "=Interviews!A2:P50"
End With

但是,我该怎么做,以便仅根据单元格是否等于某个值来填充它。

如果工作表:采访中的列:F等于textbox1.text,则仅填充此列表框。

2 个答案:

答案 0 :(得分:2)

我假设您是在谈论用户表单中的列表框,如果可以的话,希望它对您有用...

Private Sub UserForm_Initialize()
    For i = 1 To 10
        If UCase(Left(Sheet1.Cells(i, 1), 3)) = "YES" Then
            ListBox1.AddItem Sheet1.Cells(i, 1)
        End If
    Next
End Sub

enter image description here

答案 1 :(得分:1)

只是一个例子

Private Sub UserForm_Initialize()
    Set sh = ThisWorkbook.Worksheets(1)
    With sh
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    lC = 0
    With Me.ListBox1
        .ColumnCount = 2
        .RowSource = ""
        For r = 2 To LR
            If Left(sh.Cells(r, 1), 1) = "P" Then ' <<< to changed condition
                .AddItem
                .List(lC, 0) = sh.Cells(r, 1)
                .List(lC, 1) = sh.Cells(r, 2)
                lC = lC + 1
            End If
        Next
        If .ListCount = 0 Then MsgBox "No data found"
    End With
End Sub