Remove blank items from a listbox

gaudrco

Board Regular
Joined
Aug 16, 2019
Messages
203
I have a listbox that loads the list items from a range of cells. Some of the cell values in the range are blank, therefore when the code is executed, the listbox loads the blank cells in as well. I would like to see if there is a way to have the code remove the blank values from the list. I've researched this on google but haven't been able to find a way to get it to work for my code. Here is my current code:

Code:
Private Sub UserForm_Activate()
Listboxreorder.Clear
For i = 4 To 40
    If Columns(i).Hidden = False Then Listboxreorder.AddItem Cells(7, i).Value
Next
End Sub
Sub UpDate_List()
Listboxreorder.Clear


 With Listboxreorder.List
    With SpinButton1
        .Value = 0
        .Max = 1
        .Min = -1
        .SmallChange = 1
    End With
    End With
    End Sub


    Private Sub SpinButton1_SpinDown()
    MoveItem 1
End Sub


Private Sub SpinButton1_SpinUp()
    MoveItem -1
End Sub


Private Sub MoveItem(X As Long)
    Dim itms As String, i As Long, lbVal As String, newPos As Long
'move the item
    With Me.Listboxreorder
        If .ListIndex > -1 Then
            newPos = .ListIndex + X
            If newPos < 0 Or newPos = .ListCount Then Exit Sub
            lbVal = .Value
            itms = .List(newPos)
            .List(newPos) = .List(.ListIndex)
            .List(.ListIndex) = itms
        End If
're-select original item
        For i = 0 To .ListCount - 1
            If .List(i) = lbVal Then .Selected(i) = True
        Next i
     End With
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Why load all of the cells and not just the ones that contain a value?

Code:
    If Columns(i).Hidden = False [COLOR=#ff0000]And Cells(7, i) <> ""[/COLOR] Then Listboxreorder.AddItem Cells(7, i).Value
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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