Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Nested loop Programing Challenge

Hi

Your nested loops are fine, I thing problem how you 'read' the values from
your array variable.

Try this:

Sub test()
Dim listOne()
RowNum = 5
ClmNum = 5
Worksheets("Sheet2").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
For c = LBound(listOne) To UBound(listOne)
Worksheets("Sheet4").Range("A1").Offset(off, 0) = listOne(c)
off = off + 1
Next
End Sub

Regards,
Per


"Benjamin Fortunato" skrev i
meddelelsen ...
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!


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Do Until Loop PJFry Excel Programming 4 September 20th 07 09:10 PM
Nested loop [email protected] Excel Worksheet Functions 1 April 5th 07 12:50 AM
A challenge: referencing cell contents in nested function formula Needs Assistance Excel Worksheet Functions 3 October 28th 06 02:17 AM
A challenge: referencing cell contents in a nested function formu Needs Assistance[_2_] Excel Programming 0 October 28th 06 01:10 AM
Nested Loop Frank Kabel Excel Programming 6 September 12th 04 02:41 AM


All times are GMT +1. The time now is 03:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"