help with array, ifthen, loop

[code]
2d_array = getrows()

'2d_array(1,20)
'column1, column2
'1,1
'1,2
'1,3
'1,1
'1,2
'1,3
'1,1
'1,2
'1,3
'2,1
'2,2
'2,3
'2,1
'2,2
'2,3
'2,1
'2,2
'2,3
'2,1
'2,2
'2,3

a = 0
b = 0
Redim zz(0,0)
Do While a < Ubound(2d_array,2)
      If 2d_array(1,a) = 1 Then
            If PreviousRow <> "" Then PreviousRow = 2d_array(0,a)

            If PreviousRow = 2d_array(0,a) Then
              Redim preserve zz(0,b)
              zz(0,b) = zz(0,b) + 1
            End If

            If PreviousRow <> 2d_array(0,a) Then
                b = b + 1
              Redim preserve zz(0,b)
              zz(0,b) = 1
              PreviousRow = 2d_array(0,a)
            End If
      End If
      a = a + 1
Loop

[/code]

I'm trying to make this work but it doesn't seem to want to follow my (flawed) logic. As you can see, I am using the 2nd column as the main counter. Whenever 2d_array(1,a) = 1, the IfThen statements will be exectued.

When it first loops, the code:

If PreviousRow <> "" Then PreviousRow = 2d_array(0,a)

will initialize PreviousRow to the first 2d_array row. So PreviousRow would equal to 1 in this case upon the first loop.

The 2nd If statement:

            If PreviousRow = 2d_array(0,a) Then
              Redim preserve zz(0,b)
                  zz(0,b) = zz(0,b) + 1
            End If

Since PreviousRow = 1 and 2d_array(0,0) = 1 also then zz(0,b) = zz(0,b) + 1. So actually zz(0,0) = 1 at this point.

The 3rd If statment:

            If PreviousRow <> 2d_array(0,a) Then
                  b = b + 1
              Redim preserve zz(0,b)
                  zz(0,b) = 1
                  PreviousRow = 2d_array(0,a)
            End If

Since PreviousRow = 1 and 2d_array(0,0) = 1 are the same, this code is not executed until 2d_array(0,9). If it finds a difference, then it starts a new row.

Finally, the final output of the zz array should be:

zz(0,0) = 3
zz(0,1) = 4


Right now my incorrect output is:

zz(0,0) = "" (nothing)
zz(0,1) = 7



Hopefully I made myself clear...
fcqmaxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I think this does what you want...

Option Explicit

Private my2dArray(1, 20) As Integer
Private zz() As Integer

Private Sub Form_Load()
    Dim i As Integer
    Dim cnt As Integer
    For i = 0 To 20
        If i <= 8 Then
            my2dArray(0, i) = 1
        Else
            my2dArray(0, i) = 2
        End If
       
        cnt = cnt + 1
        If cnt = 4 Then
            cnt = 1
        End If
        my2dArray(1, i) = cnt
    Next i
   
    Debug.Print "my2dArray"
    For i = 0 To UBound(my2dArray, 2)
        Debug.Print i & ": " & my2dArray(0, i) & "," & my2dArray(1, i)
    Next i
    Debug.Print
End Sub

Private Sub Command1_Click()
    Dim i As Integer
    Dim PreviousRow As Integer
    Dim cnt As Integer
    Dim rows As Integer
   
    ReDim zz(0, 0)
    PreviousRow = -1
    rows = -1
    For i = 0 To UBound(my2dArray, 2)
        If my2dArray(1, i) = 1 Then
            If PreviousRow = -1 Then
                PreviousRow = my2dArray(0, i)
                cnt = 1
            ElseIf my2dArray(0, i) = PreviousRow Then
                cnt = cnt + 1
            Else
                PreviousRow = my2dArray(0, i)
                rows = rows + 1
                ReDim Preserve zz(0, rows)
                zz(0, rows) = cnt
                cnt = 1
            End If
        End If
    Next i
    rows = rows + 1
    ReDim Preserve zz(0, rows)
    zz(0, rows) = cnt
   
    Debug.Print "zz()"
    For i = 0 To UBound(zz, 2)
        Debug.Print zz(0, i)
    Next i
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.