Strange listbox problem

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
I’ve created a listbox. I’ve added one item to that listbox.

The listcount property = 1, but looping through each item in the list executes ten cycles instead of one (I’ve used a message box to confirm this).<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
How can this be?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
'Two userforms.
'1) Contains a textbox
'2) Contains one listbox

'Userform1:

Code:
Private Sub commandbutton1_click()
job = TextBox1
If Not Join(r) = "" Then
For Each thing In r()
If thing = job Then
JobPresent = MsgBox("Redundant entry.", vbOKOnly, "Redundant Entry")
Exit Sub
End If
Next
End If
If IsEmpty(x) Then
p = p + 1
ReDim Preserve r(1 To p)
r(p) = job
'MsgBox Join(r, Chr(10))
Else
x = x + 1
ReDim Preserve r(1 To x)
r(x) = job
'MsgBox Join(r, Chr(10))
End If
End Sub


'UserForm 2:
Code:
Private Sub UserForm_Initialize()
'==================================================
'used to fill the listbox from the array r()
If Join(r) = "" Then
    MsgBox "Join(r) = """""
    Exit Sub
End If
For Each element In r()
    v = v + 1
'    If Not IsEmpty(element) Then
        ListBox1.AddItem r(v)
'    End If
Next
'==================================================
'Purely for test
For Each thing In ListBox1.List
asd = asd + 1
Next
For Each thing In r()
asdf = asdf + 1
Next
MsgBox "ListBox1.ListCount = " & asd & Chr(10) & "Array r() = " & asdf & elements
'==================================================
End Sub


'Standard Module:

Code:
Public r()
Public Sub test9999()
UserForm1.Show
End Sub
 
Upvote 0
Start by entering some text in the ugly textbox, click the big command button on the top, then click the little one on the bottom.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
The second form loads and appears. A messagebox appears which reveals the listbox count and the number of elements in r().<o:p></o:p>
<o:p> </o:p>
r() contains 1 element, and the listboxcount is 10 for some weird reason.<o:p></o:p>
<o:p> </o:p>
Any ideas?<o:p></o:p>
 
Upvote 0
Why don't you clear the listbox before you start adding items to it?
 
Upvote 0
Hence the other thread you responded to last night.

I've already tried doing that, using the method you posted in that other thread (Clear). It doesn't solve the problem.

There are still ten items in the listbox list, even though the code explicitly adds only non-empty elements from the array, and the array contains just one element if you press commandbutton1 a single time.

Did you work at all with the example I posted? I'm able to reproduce this apparent bug without issue.
 
Upvote 0
It appears that the default array size of the .List for a ListBox is a zero based array of (0, 9) for each item added to the list. That is...
one item added: .List array = 0,9
two items added: .List array = 1,9
three items added: .List array = 2,9

Why it has a 2nd dimension default to 10 elements, I don't know. There's probably a reason (maybe legacy).

So when you use...
For Each thing In ListBox1.List
... that will loop through all 10 elements (zero to 9) for each entry in the .List.
Doesn't matter if you filled all 10 elements or not.

You could use this instead...
Code:
[COLOR="Green"]'Purely for test[/COLOR]
For i = 0 to ListBox1.ListCount - 1
asd = asd + 1
Next i


This is the test I used...
Code:
Private Sub UserForm_Initialize()

    n = 5

    With ListBox1
    
    'Add items to list
    For i = 1 To n
        .AddItem "Item " & i
    Next i
    
        For i = 0 To .ListCount - 1     ' Count items
            asd = asd + 1
        Next i
        For Each element In .List       ' Count elements
            elCount = elCount + 1
        Next element
        
    MsgBox ".List array size = " & UBound(.List, 1) & "," & UBound(.List, 2) & vbCr & _
           ".List total number of elements = " & elCount & vbCr & _
           ".ListCount property = " & .ListCount & vbCr & _
           ".List counted by loop = " & asd, _
           Title:=n & " Items Added"

    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top