How can i amend this code to go in listbox

Eisasuarez

Well-known Member
Joined
Mar 23, 2012
Messages
653
Hi

How can i amend this so the results go into a list box rather than the sheet cells?

I have 4 columns

Private Sub CmdFilter_Click()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
ws.Range("A5:D17").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("F5:F6"), _
CopyToRange:=Range("H5:K5"), _
Unique:=False
End Sub


Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi

How can i amend this so the results go into a list box rather than the sheet cells?

I have 4 columns

Private Sub CmdFilter_Click()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
ws.Range("A5:D17").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("F5:F6"), _
CopyToRange:=Range("H5:K5"), _
Unique:=False
End Sub


Thanks
Any luck from one of you lovely people
 
Upvote 0
Try:

Code:
Private Sub CmdFilter_Click()
    Dim ws As Worksheet
    Dim LR As Long
    Set ws = Sheets("Sheet1")
    With ws
        .Range("A5:D17").AdvancedFilter _
            Action:=xlFilterCopy, _
            CriteriaRange:=.Range("F5:F6"), _
            CopyToRange:=.Range("H5:K5"), _
            Unique:=False
        LR = .Range("H5").End(xlDown).Row
        ListBox1.List = .Range("H5:K" & LR).Value
    End With
End Sub
 
Upvote 0
Thank you Andrew

can you please advise if this is correct

1) if i am transferring data from sheet to listbox or transferring multiple columns of data, use this method
listbox.list=

when you use add item?
 
Upvote 0
You can use AddItem, but then you would have to populate the remaining columns. Using List is a lot easier.
 
Upvote 0
Thats cool
i wernt sure when to use one over the other

i guess the way to go with list box is use list and not additem
 
Upvote 0
Hi Andrew

just another quick question

say i was looping in a range (a1:a10) and i was using an if statement to see if the cell value = Apple (say A4 contained Apple, then copy (A4:C4) to the list box

Can i use list?

i know how to use an if statement and loop

for each cell in range("a1:a10")
If cell.value = "apple" then
listbox.list = ????
end if
next cell
 
Upvote 0
Try:

Code:
Private Sub CmdFilter_Click()
    Dim ws As Worksheet
    Dim LR As Long
    Set ws = Sheets("Sheet1")
    With ws
        .Range("A5:D17").AdvancedFilter _
            Action:=xlFilterCopy, _
            CriteriaRange:=.Range("F5:F6"), _
            CopyToRange:=.Range("H5:K5"), _
            Unique:=False
        LR = .Range("H5").End(xlDown).Row
        ListBox1.List = .Range("H5:K" & LR).Value
    End With
End Sub

Hi Andrew

There is only one problem with this

H5:K5 are my headers and i have set the property of the listbox column header to true and column count to 3 however it is not showing the first row (H5:K5) as my column Header
 
Upvote 0
To show a header you need to set the RowSource property:

Code:
ListBox1.RowSource = "'" & .Name & "'!" & .Range("H6:K" & LR).Address(0, 0)
 
Upvote 0

Forum statistics

Threads
1,203,318
Messages
6,054,712
Members
444,743
Latest member
johncon60

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top