Populate Listbox if value exists

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I have a Listbox on a userform and I want to load it with values from a range but only if another column has a specific value.

My range is C2:C42 - I need the values to be loaded only if the text "Unassigned" does not appear on the same row in H2:H42.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
Code:
Private Sub UserForm_Initialize()
   Dim Cl As Range
   Dim Lst As String
   For Each Cl In Range("C2:C42")
      If Cl.Offset(, 5).Value <> "Unassigned" Then
         If Lst = "" Then
            Lst = Cl.Value
         Else
            Lst = Lst & "," & Cl.Value
         End If
      End If
   Next Cl
   ListBox1.List = Split(Lst, ",")
End Sub
 
Upvote 0
Fluff,

Thanks it works, but there is a but.....

It's showing blank lines where there are blank values - sorry, I should have mentioned that this could be the case - is there a workaround?
 
Upvote 0
Try
Code:
Private Sub UserForm_Initialize()
   Dim Cl As Range
   Dim Lst As String
   For Each Cl In Range("C2:C42")
      If Cl <> "" And Cl.Offset(, 5).Value <> "Unassigned" Then
         If Lst = "" Then
            Lst = Cl.Value
         Else
            Lst = Lst & "," & Cl.Value
         End If
      End If
   Next Cl
   ListBox1.List = Split(Lst, ",")
End Sub
 
Upvote 0
For the given fixed ranges, I think this non-looping UserForm_Initialize event will also work...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub UserForm_Initialize()
  Dim UnusedCol As Long
  UnusedCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious, , , False).Column + 1
  Intersect(Rows("2:42"), Columns(UnusedCol)) = [IF((H2:H42<>"Unassigned")*(C2:C42<>""),"X","")]
  Intersect(Columns("C"), Columns(UnusedCol).SpecialCells(xlConstants).EntireRow).Copy Cells(1, UnusedCol + 2)
  ListBox1.List = Application.Transpose(Cells(1, UnusedCol + 2).CurrentRegion)
  Columns(UnusedCol).Resize(, 3).Clear
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi Rick,

Thanks - but I'm getting a run time error 1004 - 'No Cells were found'

I've pasted your suggestion exactly so not sure what the problem is, do you?
 
Upvote 0
In fact, ignore my last - it was a very minor typo on my part.

Works a treat - thanks to you both.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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