Solved

# help with array, ifthen, loop

Posted on 2006-03-26
163 Views
Last Modified: 2010-04-07
[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
Question by:fcqmax
1 Comment

LVL 85

Accepted Solution

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

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

#### 746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!