ListBox population problem...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,707
Hi,

I'm using the following code to populate a ListBox:

Code:
    LR = Sheet2.Range("D" & Rows.Count).End(xlUp).Row
    
    With lbxTest
        .Clear
        For i = 1 To LR
            .AddItem Sheet2.Range("D" & i).Value
        Next i
    End With
On occasions, column D of Sheet2 will be empty. When this happens, I want my ListBox to be empty, i.e. no tick boxes.

At the moment a single tick box is added when column D is empty, so how can I prevent this from happening?

Cheers,

Matty
 

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,707
Hi,

Thanks for the link. It looks a useful resource, but I can't see anything to handle my particular issue.

Anyone any other ideas?

Cheers,

Matty
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,317
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
I'd suggest just checking D1:

Code:
    LR = Sheet2.Range("D" & Rows.Count).End(xlUp).Row
    lbxTest.Clear
    If LR > 1 then
    lbxTest.List Sheet2.Range("D1:D" & LR).Value
     Else
        if len(Sheet2.Range("D1").Value <> 0 then lbxTest.AddItem Sheet2.Range("D1").Value
    End If
 

Forum statistics

Threads
1,081,544
Messages
5,359,432
Members
400,528
Latest member
Ratish52

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top