Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

remove blanks from listbox vba

Posted on 2014-03-31
7
Medium Priority
?
3,047 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

971 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