Accessing columns of a listbox

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Accessing columns of a listbox

Post by Pat »

I have a listbox with 6 columns. In code i wish to select a certain column for a given row.
How do i do this?
If i use ListBox.Column(1, ID) i get column 1's name. I want the value in column 1 in row ID.

User avatar
mbarron
2StarLounger
Posts: 112
Joined: 25 Jan 2010, 20:19

Re: Accessing columns of a listbox

Post by mbarron »

Try
ListBox.Column(0,ID)

I used the following to list all the values of all rows and columns

Code: Select all

Private Sub btnRun_Click()
Dim i As Integer, j As Integer
For i = 0 To List2.ListCount - 1
    For j = 0 To List2.ColumnCount - 1
        Debug.Print List2.Column(j, i)
    Next
Next
End Sub

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Accessing columns of a listbox

Post by Pat »

Interesting, i copied your code and got the same results as you did. My listbox name is deplist.
How ever the code:
deplist.Column(cDeptID, ID)
returns the Column Heading text (eg, "DepartmentID") and not the value.

I'm nonplussed. I must be missing something.

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Accessing columns of a listbox

Post by HansV »

Have you set the Column Heads property of the list box to Yes?

If so, row 0 contains the column headings. To get the value of the m-th column in the n-th row, you would need to use

depList.Column(m - 1, n)
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Accessing columns of a listbox

Post by Pat »

Silly me, that's right Hans, i had set the Column Heads to Yes.