Solved

remove blanks from listbox vba

Posted on 2014-03-31
7
2,062 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
Comment Utility
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
Comment Utility
thomas
Still not deleting.
listbox-nullValues.png
0
 
LVL 3

Author Comment

by:fordraiders
Comment Utility
.List(z)= null Then
will not work either ?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 39

Expert Comment

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

Author Comment

by:fordraiders
Comment Utility
this take everything out ?

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

Author Comment

by:fordraiders
Comment Utility
'' 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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now