vbaexcel

Stop blank cells entering into listbox


I have a listbox which is populated with a range of cells but unfortunately some of the cells are blank. In some cases my listbox is getting populated with a large number of cells so it would not be practical to enter each cell manually. Below shows the line of code that populates the listbox:

Me.ListBox1.List = Sheets("List Names").Range("C1022:C1200").Value

but I need a way to loop through the cells and only add cells that have information in them, or is that even possible. Thanks in advance.


Solution

  • You sure can loop cells to fill your listbox. You need to do something like:

    sub jzz
    Dim myRange As Range
    Dim myCell As Range
    
    Set myRange = Range("A1:A5")
    
    For Each myCell In myRange
        If myCell.Value <> vbNullString Then
            Me.ListBox1.AddItem myCell.Value
        End If
    Next myCell
    end sub