Solved

remove blanks from listbox vba

Posted on 2014-03-31
7
2,400 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

830 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