Pick List with Criteria - Collection - Fill Form Picklist

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
I am looking for a way to fill a collection with values only if the value in the next column is a 1. I currently have this code which works great but I don't know how to give it criteria like a "where" statement that checks the next columns value.

Code:
    Dim AllCells As Range
    
'   The items are in A1:A20
    Set AllCells = Range("A1:A20")
    'Only input values if the value in column B is 0

Any help?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Here is my full code for review

Code:
Sub populatelist()

    Dim AllCells As Range
    Dim Cell As Range
    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item
    
'   The items are in A1:A20
    Set AllCells = Range("A1:A20")
    'Only if the value in column B is 0
    

    On Error Resume Next
    For Each Cell In AllCells
        NoDupes.Add Cell.Value, CStr(Cell.Value)
    Next Cell

    On Error GoTo 0

'   Sort the collection (optional)
    For i = 1 To NoDupes.count - 1
        For j = i + 1 To NoDupes.count
            If NoDupes(i) > NoDupes(j) Then
                Swap1 = NoDupes(i)
                Swap2 = NoDupes(j)
                NoDupes.Add Swap1, before:=j
                NoDupes.Add Swap2, before:=i
                NoDupes.Remove i + 1
                NoDupes.Remove j + 1
            End If
        Next j
    Next i
    
'   Add the sorted, non-duplicated items to a ListBox
    For Each Item In NoDupes
        UserForm1.ComboBox1.AddItem Item
    Next Item

'   Show the UserForm
    UserForm1.Show
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,049
Messages
6,128,496
Members
449,455
Latest member
jesski

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