B
Benjamin Fortunato
I would like someone to explain to me how nested loops work. There does not
seem to be a lot of literature on it. I am trying to go through a worksheet
and count the number of ones. I then have to take these vales and create a
list which tallies the numeric cell value of that item. See the example below
it will clarify everything. I am using nested loops and my list generates the
same number. I believe what it is doing is taking the last value and filling
it into each cell.
Worksheet(2)
0 0 0 0 0
0 1 1 0 0
0 0 0 0 1
1 0 0 0 0
0 0 0 0 1
Worksheet(3)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Worksheet(4)
7
8
15
16
25
But I get
25
25
25
25
25
Here is the code
Worksheets(2).Activate
Set totalRange = Range("A1").Resize(RowNum, ClmNum)
OneCount = Application.WorksheetFunction.CountIf(totalRange, 1)
ReDim listOne(OneCount + 1)
i = 0
For rCnt = 1 To RowNum
For ClmCnt = 1 To ClmNum
If Worksheets(2).Cells(rCnt, ClmCnt).Value = 1 Then
listOne(i) = Worksheets(3).Cells(rCnt, ClmCnt)
i = i + 1
End If
Next
Next
Thanks!
seem to be a lot of literature on it. I am trying to go through a worksheet
and count the number of ones. I then have to take these vales and create a
list which tallies the numeric cell value of that item. See the example below
it will clarify everything. I am using nested loops and my list generates the
same number. I believe what it is doing is taking the last value and filling
it into each cell.
Worksheet(2)
0 0 0 0 0
0 1 1 0 0
0 0 0 0 1
1 0 0 0 0
0 0 0 0 1
Worksheet(3)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Worksheet(4)
7
8
15
16
25
But I get
25
25
25
25
25
Here is the code
Worksheets(2).Activate
Set totalRange = Range("A1").Resize(RowNum, ClmNum)
OneCount = Application.WorksheetFunction.CountIf(totalRange, 1)
ReDim listOne(OneCount + 1)
i = 0
For rCnt = 1 To RowNum
For ClmCnt = 1 To ClmNum
If Worksheets(2).Cells(rCnt, ClmCnt).Value = 1 Then
listOne(i) = Worksheets(3).Cells(rCnt, ClmCnt)
i = i + 1
End If
Next
Next
Thanks!