what's the meaning of listcount -1

2019-06-01 04:33发布

问题:

I've been searching on the internet trying to find what the meaning of listcount -1 is, but i can't seem to find the exact answer.

why is there -1 beside the listcount? or .list(.listcount -1,1) what is that mean?

edit

example of the listcount

.additem a.value

.list(.listcount -1, 1) = ws.cells(A,B).value

this adds the value of ws.cells(A,B) to the listbox1 but i don't understand why/how?

or i found a code on the internet http://www.ozgrid.com/VBA/multi-select-listbox.htm

i understand that the purpose of the code is "if one of the list is selected, then transfer the value of listbox into cells" but why is there .Listbox1.Listcount -1?

回答1:

To make things clearer, as I've said, ListCount starts at 1 when counting ListBox items or List.
But ListBox Indexing of those items or List starts at 0. In your example, you are trying to add an item or List in a Multi-Column ListBox.
Take a look at below example:

With me.ListBox1
    .Add "Item1"
    .List(.ListCount - 1, 1) = "Item2"
    .List(.ListCount - 1, 2) = "Item3"
End With

Above code populates a ListBox initially set with 3 columns.
This line .Add "Item" adds the first List in the ListBox.
But you need to populate the rest of the columns.
To access those columns, you use .List property with this syntax:

expression.List(pvargIndex, pvargColumn)

pvargIndex is the ListBox ListIndex and pvargColumns is the column number.
Remember that you already added 1 item using the .Add property and the ListIndex of that item is?

.ListCount - 1 'which is current item count in the ListBox less one equal to 0

To learn more about pupulating ListBox, check THIS out.
Also see HERE.
Hope above helps you somehow.