[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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...
0
fcqmax
Asked:
fcqmax
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now