Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Listbox Looping

I have a listbox with 2 columns. First column is EmployeeID and second column
is Employee name. what is the best way to loop thru this list box and
capture each ID and name in a variable?

thanks for your assistance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Listbox Looping

Is your listbox set up for multiselect = fmMultiSelectMulti and you want to get
the selected items?

This worked for me to get an array of the selected items:

Option Explicit
Private Sub CommandButton1_Click()
Dim iCtr As Long
Dim sCtr As Long
Dim myArr() As String

With Me.ListBox1
ReDim myArr(0 To .ColumnCount - 1, 0 To .ListCount - 1)

sCtr = -1
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) = True Then
sCtr = sCtr + 1
myArr(0, sCtr) = .List(iCtr, 0)
myArr(1, sCtr) = .List(iCtr, 1)
End If
Next iCtr

If sCtr = -1 Then
MsgBox "Nothing selected!"
Else
ReDim Preserve myArr(0 To .ColumnCount - 1, 0 To sCtr)
'prove that it worked
For iCtr = 0 To sCtr
MsgBox myArr(0, iCtr) & "--" & myArr(1, iCtr)
Next iCtr
End If

End With
End Sub
Private Sub UserForm_Initialize()

Dim iCtr As Long

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.ColumnCount = 2
For iCtr = 0 To 9
.AddItem "A" & iCtr
.List(iCtr, 1) = "B" & iCtr
Next iCtr
End With
End Sub


Bret wrote:

I have a listbox with 2 columns. First column is EmployeeID and second column
is Employee name. what is the best way to loop thru this list box and
capture each ID and name in a variable?

thanks for your assistance.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping to fill ListBox davidm Excel Programming 2 January 5th 06 08:39 AM
VBA: Creating listbox similar to the one in Pivot table (Listbox+Checkbox) modjoe23 Excel Programming 3 August 18th 05 02:35 PM
Looping through listbox controls Vince Excel Programming 11 April 12th 05 04:40 PM
Excel VBA-Looping though MultiSelect on ListBox jpendegraft[_3_] Excel Programming 2 February 3rd 04 04:38 PM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM


All times are GMT +1. The time now is 01:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"