Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Nested Do Until Loop | Excel Programming | |||
Nested loop | Excel Worksheet Functions | |||
A challenge: referencing cell contents in nested function formula | Excel Worksheet Functions | |||
A challenge: referencing cell contents in a nested function formu | Excel Programming | |||
Nested Loop | Excel Programming |