Solved

remove blanks from listbox vba

Posted on 2014-03-31
7
2,287 Views
Last Modified: 2014-04-01
vba 2010 excel.
userform.

I have a listbox.
That contains blanks items in the listbox

The code below will help get rids of dups in the listbox.
But I need to just get rid of all blanks or null values from a listbox.

I believe i just check for
If .List(z) = "" and/or If .List(f) = ""

But trying to figure out how to nest it here in the code ?



With UserForm2.ListBox49
        For f = 0 To .ListCount - 1
            For z = .ListCount To (f + 1) Step -1
                If .List(z) = .List(f) Then
                    .RemoveItem z
                End If
            Next
        Next
    End With

Open in new window




Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 5
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 39967648
Try this update to your code, adding a "or blank" condition in your if statement.

With UserForm2.ListBox49
        For f = 0 To .ListCount - 1
            For z = .ListCount To (f + 1) Step -1
                If .List(z) = .List(f) or .List(z)="" Then
                    .RemoveItem z
                End If
            Next
        Next
    End With

Open in new window


Thomas
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39967738
thomas
Still not deleting.
listbox-nullValues.png
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39967781
.List(z)= null Then
will not work either ?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 39

Expert Comment

by:nutsch
ID: 39967833
Have you tried ".List(z) Is null" ?
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39968446
this take everything out ?

".List(z) Is null"
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39970067
'' loop get rid of blanks
 Dim l As MSForms.ListBox: Set l = UserForm2.ListBox49
  Dim i As Long: i = 0

 '' remove anything that looks like a blank
  While i < l.ListCount
    If "" = Trim$(l.List(i, 0)) Then: l.RemoveItem (i): Else i = 1 + i
  Wend


this works.
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39970136
Thanks



 .List(z)="" Then


added reverse solution here:
'' loop get rid of blanks
 Dim l As MSForms.ListBox: Set l = UserForm2.ListBox49
  Dim i As Long: i = 0

 '' remove anything that looks like a blank
  While i < l.ListCount
    If "" = Trim$(l.List(i, 0)) Then: l.RemoveItem (i): Else i = 1 + i
  Wend
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question