VBA -- Filtered Cells Value to a Listbox

Koth851

New Member
Joined
Nov 26, 2018
Messages
2
Good Afternoon,
I try to optimize my master excel file by adding some macros. I looked over different forums, but I still don't understand why my code doesn't work.

I click on the commandbutton: CommandButton_Stage1.

First, it has to remove every filter.
Then, I want to filter the data at the cell "A4". (sheet Projects_List)

After, I want to use that filtered information (under the cell "C4") into a Listbox.
I want to expand the cell selection until it reaches a blank cell.

Here's my code:

Private Sub CommandButton_Stage1_Click()


Sheets("Projects_List").Select
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("$A$4:$J$20").AutoFilter Field:=1, Criteria1:="1"




Set rRange = Range("C4", Range("C4").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)


ListBox_Projects.RowSource = rRange.Address

End Sub

Thanks for your help,
Its appreciated.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can't fill the 'Rowsource' property with a filtered range.
There's no straightforward way to populate a listbox with a filtered range. If you're filtered range is small, say about 1000 row, then
using 'ListBox_Projects.Additem' method will be fast enough.
But if your data is big then we need another approach.


Code:
Private Sub CommandButton_Stage1_Click()

Sheets("Projects_List").Select
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("$A$4:$J$20").AutoFilter Field:=1, Criteria1:="1"

Set rRange = Range("C4", Range("C4").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
ListBox_Projects.Clear
For Each c In rRange
ListBox_Projects.AddItem c
Next

End Sub

Another note:
you may want to change this part:
Code:
Set rRange = Range("C4", Range("C4").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
to this:
Code:
Set rRange = Range("C4", Cells(Rows.count, "C").End(xlUp)).SpecialCells(xlCellTypeVisible)

Using 'End(xlDown)' is not a reliable way to find the last row with data.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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