ListBox for Specific Condition

IoanZ

Board Regular
Joined
Mar 2, 2018
Messages
51
Hellow to everyone.

Please help me.

I have this Range("A2:D4") with fruits and quantity

To load this Range in a Listbox Userform i use this code

Private Sub UserForm_Initialize()
ListBox1.RowSource = Sheets("Sheet1").Range("A2:D4").Address
End Sub


I want to know which is the vba code to put in this ListBox only the products with the quantity greater than 0 ( qant > 0 ) ?

Thank's a lot.

Row/CollACD
1ProductQuantityunit
2Apples20kg
3Oranges0kg
4Bananas25kg
5

<tbody>
</tbody>
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can use a loop to identify what values to add to the listbox.

Code:
Private Sub UserForm_Initialize()
Dim arr As Variant
Dim r&, c&, n&
arr = [A2:D4]
ListBox1.Clear  'remove items
For r = LBound(arr, 1) To UBound(arr, 1)
    If arr(r, 3) > 0 Then
        ListBox1.AddItem arr(r, 1) 'add 1st item of "row"
        For c = 2 To 4
            ListBox1.List(n, c - 1) = arr(r, c) 'add add'l "columns"
        Next c
        n = n + 1
    End If
Next r
End Sub

For more info: https://excelmacromastery.com/vba-user-forms-2/#The_ListBox
 
Upvote 0
@IoanZ
For future reference
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Hello I did not know that I will read the rules with great care Thank you for your answer
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,269
Members
449,093
Latest member
Vincent Khandagale

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