Solved

remove blanks from listbox vba

Posted on 2014-03-31
7
2,542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
78 files, need to delete row 2 in every file 3 33
autofill formulas using macro 8 54
Excel to show a dynamic Picklist at level2 2 23
Cell Manipulation 37 51
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
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;…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

739 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