populate ListBox depending on values in two columns

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi,
I am trying to populate a listbox in a userform that ignores the value 0 in two colums C & D so it only populates the listbox with the rows that have a value greater than 0 in columns C & D. The image is an example of the data so what I would need from that is to just show the two rows. Hope this makes sense.

1602763891609.png


Regards

Also posted here Populate listbox in userform from worksheet ignoring 0 from two columns - OzGrid Free Excel/VBA Help Forum
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
In your example:
populates the listbox with the rows that have a value greater than 0 in columns C & D
I only see row 2 which would meet this test

And are you saying you are using a multicolumn Listbox?
 
Upvote 0
Hi,

I see what you mean and the way I want it will not work. The image below is what I would need it to populate from the data, is there a way to do this by colour of cells may be ? or may be another way.

1602766976580.png


Regards
 
Upvote 0
Hi,

I see what you mean and the way I want it will not work. The image below is what I would need it to populate from the data, is there a way to do this by colour of cells may be ? or may be another way.

View attachment 24259

Regards
So in this example what values would be loaded into the listbox?
And I asked are you using a multicolumn listbox and you did not answer that question
 
Upvote 0
Hi,

Both rows would be loaded into the listbox and all four columns if that is what you mean by multicolumn.

Regards
 
Upvote 0
Hi,

Both rows would be loaded into the listbox and all four columns if that is what you mean by multicolumn.

Regards
Most listboxes only show one piece of data per row.
A multicolumn listbox can have many pieces of data per row just like a worksheet has numerous columns of data.

Now why are you saying both rows would be loaded into listbox?
So all rows in your worksheet will be loaded into the listbox columns 1 to 4

What do you plan to do with this data after you get it into the userform listbox?

Working with multicolumn listboxes is a little complicated so do you know what to do after you get the data into the listbox?
 
Upvote 0
In the example data below the only rows to be loaded into the listbox would be rows 2 & 5 columns 1 to 4 as they contain a value greater than 0 in either column C or D rows 3 & 4 would be ignored.

1602770506757.png


The listbox will just sit on a homepage userform showing what Core is still to come back in.
 

Attachments

  • 1602770219903.png
    1602770219903.png
    8.6 KB · Views: 4
Upvote 0
Assuming the picture with all columns having headers is correct,
try this
VBA Code:
Private Sub UserForm_Initialize()
    Dim lr As Long, lc As Long
    Dim filtrng As Range
    
Application.ScreenUpdating = False

With Sheet1
    'remove any existing filters
    If .FilterMode Then .ShowAllData
    'determine last row
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    'determine last column
    lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
    'put formula in next column
    Range(.Cells(2, lc + 1), .Cells(lr, lc + 1)).FormulaR1C1 = "=RC[-2]+RC[-1]"
    'filter on new column
    Set filtrng = .Cells(1).CurrentRegion
    With filtrng
        .AutoFilter field:=lc + 1, Criteria1:="<>0"
    End With
End With
    
'add temp sheet to copy to
Sheets.Add
With ActiveSheet
    .Name = "Scratchpad"
    filtrng.Offset(1).Resize(lr - 1, lc).Copy
    .Paste
    'populate list box
    Me.ListBox1.List = Sheets("Scratchpad").Cells(1).CurrentRegion.Value
    'remove sheet
    Application.DisplayAlerts = False
    Sheets("Scratchpad").Delete
    Application.DisplayAlerts = True
End With

'restore sheet1 to original
With Sheets("Sheet1")
    'remove filter
    filtrng.AutoFilter
    'If .FilterMode Then .ShowAllData
    'clear the added column
    .Columns(lc + 1).ClearContents
End With
            
Application.ScreenUpdating = True

End Sub

sample file at Box.com

Hope that helps
 
Upvote 0
You're welcome, glad to have helped.

Are you satisfied with your Searchable ComboBox question?
You never responded to my questions on it.
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,830
Members
449,190
Latest member
rscraig11

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