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!

Learning Arrays.. Cant get it working :( 1

Status
Not open for further replies.

sgkdnay

Technical User
Aug 4, 2008
21
Tried to find the "proper" way to do arrays, but for the life of me, couldnt get it to display on my listbox test. Trying to learn few things here and there. Hope you could help shed some light here.

Private Sub arraytest()
'On Error Resume Next
Dim Sta() As String, lsta As Long
Dim ABC() As String, labc As Long
Dim i As Integer
Sta() = Array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")
ABC() = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
i = 0
For lsta = LBound(Sta) To UBound(Sta)
For labc = LBound(ABC) To UBound(ABC)
i = i + 1
List1.Text = i & " - " & lsta & " : " & labc
Next labc
Next lsta
End Sub

What did i do wrong?

Thanks
 

It is kind of hard to know what you are trying to do, but try this:
Code:
    Dim Sta() As String, lsta As Long
    Dim ABC() As String, labc As Long
    Dim i As Integer
    
    Sta = Split("AL,AK,AZ,AR,CA,CO,CT,DC,DE,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY", ",")
    ABC = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
    i = 0
    For lsta = LBound(Sta) To UBound(Sta)
        For labc = LBound(ABC) To UBound(ABC)
            i = i + 1
            List1.AddItem i & " - " & lsta & " : " & labc
        Next labc
     Next lsta

It works, just don't know what it is supposed to do.

Have fun.

---- Andy
 
Wow, something new to me, result came out like:

1 - 0 : 0
2 - 0 : 1
...
1325 - 50 : 24
1326 - 50 : 25
so on

Almost like what I wanted, however would like to display something like

1 - AL : A
2 - AL : B
...
1325 - WY : Y
1326 - WY : Z

Hope this is clear to you..
 
I found it, you gave me plenty idea how to tweak it and this one works but wont work as SUB only FUNCTION:

Private Function arraytest(ParamArray values() As Variant) As Integer()
'On Error Resume Next
Dim Sta() As Variant, lSta As Long
Dim ABC() As Variant, lABC As Long
Dim i As Integer
Sta() = Array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")
ABC() = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
i = 0
List1.Clear
For lSta = 0 To UBound(Sta)
For lABC = 0 To UBound(ABC)
i = i + 1
List1.AddItem i & " - " & Sta(lSta) & " : " & ABC(lABC)
Next 'labc
Next 'lsta
End Function

Many thanks :D
 

I am glad you have it working....

I just don't know why this will not work as a SUB,
because you use it as Function, but you don't use passed ParamArray values() and you do not assign anything back to arraytest

Have fun.

---- Andy
 
yup just learned the hard way LOL

when i use sub without those "ParamArray values()" it works fine, the result came out just the same as I mentioned above with function.. as i said, Im still learning :D

Thanks again
 
And just for fun, here's an alternative way of walking through an array:
Code:
[blue]    Dim spoon As Variant
    Dim wombat As Variant
    Dim i As Long
    List1.Clear
    For Each spoon In Array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")
        For Each wombat In Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
            i = i + 1
            List1.AddItem i & " - " & spoon & " : " & wombat
        Next
    Next[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top