开发者

Excel VBA: Listbox

Let's say I have a range of values as such:

Customer | Services | Cost  | Paid
Mel      | Abc      | $1.00 | TRUE
Mel      | Def      | $2.00 | FALSE
Xin      | Abc      | $3.00 | TRUE
Titus    | EEE      | $4.00 | TRUE

and I want these items to be inserted into a listbox. However I have a few criteria, which is to display the items only specific to the user (e.g. Mel or Xin or Titus), and display only when "False". H开发者_Go百科ow am I to do so, thanks in advance.

What I have now:

Dim lbtarget As MSForms.ListBox
Dim rngSource As Range
Set rngSource = Range("Table1")    
Set lbtarget = Me.ListBox1
With lbtarget
    'Determine number of columns
    .ColumnCount = 4
    'Set column widths
    .ColumnWidths = "50;80;100"
    'Insert the range of data supplied
    .List = rngSource.Cells.Value
End With


Replace

.List = rngSource.Cells.Value

with

For Each rw In rngSource.Rows
    If rw.Cells(1,1) = <Specify User> And rw.Cells(1,4) = FALSE Then
        .AddItem ""
        For i = 1 To .ColumnCount
            .List(.ListCount - 1, i - 1) = rw.Value2(1, i)
        Next
    End If
Next

Whole Sub using Mel as the user

Private Sub CommandButton1_Click()
    Dim lbtarget As MSForms.ListBox
    Dim rngSource As Range
    Dim rw As Range
    Dim i As Long

    Set rngSource = Range("Table1")
    Set lbtarget = Me.ListBox1
    With lbtarget
        .ColumnCount = 4
        .ColumnWidths = "50;80;100"
        For Each rw In rngSource.Rows
            If rw.Cells(1, 1) = "Mel" And rw.Cells(1, 4) = False Then
                .AddItem ""
                For i = 1 To .ColumnCount
                    .List(.ListCount - 1, i - 1) = rw.Cells(1, i)
                Next
            End If
        Next
    End With

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜