Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through a list box 1

Status
Not open for further replies.

samantha72

Programmer
Jul 31, 2001
14
0
0
CA
hi,

i have a list box which contains a max of 7 strings. I need to loop through this list box
and store these strings in an array. Please help. Thanks

Samantha
 
something like this should do it :

Private Sub CopyToArray()

Dim myArray() As String

ReDim myArray(0 To List1.ListCount - 1) As String

For nLoop = 0 To List1.ListCount - 1
List1.ListIndex = nLoop
myArray(nLoop) = List1
Next

End Sub
 
Hi There

Here is an example....
Paste a command button on a form and use the following code
This code will put the number of items in a list in an array.....

Private Sub Form_Load()
With List1
.AddItem "String 1"
.AddItem "String 2"
.AddItem "String 3"
.AddItem "String 4"
.AddItem "String 5"
End With
End Sub
Private Sub Command1_Click()
Dim arrStrings() As String
Dim sngListCounter As Single
ReDim arrStrings(List1.ListCount - 1)
For sngListCounter = 0 To List1.ListCount - 1
arrStrings(sngListCounter) = List1.List(sngListCounter)
Next sngListCounter
End Sub

Cheers
Toyman
 
Or think laterally, and use the ListBox from Microsoft Forms. Then you can do (using dannyh's routine as the basic model):

Private Sub CopyToArray()

Dim myArray() As Variant
ReDim myArray(0 To ListBox1.ListCount - 1) As Varianr
myArray = ListBox1.List

End Sub

(check the documentation though, as there are some minor gotchas)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top