Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Counting Loop - Please help

I'm working on a forecasting program for my Industrial Engineering Senior
Design group. We need a function that can count in the following way.

Given the column:

1
3
4
2
0
0
2
5
0
4

This program should return

1
1
1
1
0
0
3
1
0
2

What this program is doing is looking at each cell in the column. If the
cell is zero, the function will return a 0 for the corresponding cell and
look forward to the next cell. If it is zero again, it will return a 0 and
continue in the loop until it reaches a nonzero. Upon reaching a nonzero, it
will count itself and the number of zero cells before it and return that
value. If there is a nonzero cell proceeded by a nonzero cell, the function
will simply return one.

I know this sounds crazy but we are desperate for help.

Thanks.

-
Zachary
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 364
Default Counting Loop - Please help

Assuming your values are in Col A starting at A1 and there is nothing else on
the sheet then try this macro;
'--------------------------
Sub CountZero()
Dim i
Dim j
i = 0
j = 0
LastRow = Range("A65536").End(xlUp).Row
If ActiveSheet.Cells(1, 1) = 0 Then
ActiveSheet.Cells(1, 2) = 0
Else
ActiveSheet.Cells(1, 2) = 1
j = 1
End If

For i = 2 To LastRow
If ActiveSheet.Cells(i, 1) = 0 Then
j = j + 1
ActiveSheet.Cells(i, 2) = 0
Else
ActiveSheet.Cells(i, 2) = j
j = 1
End If
Next
End Sub
'--------------------------

"Zachary Baker" wrote:

I'm working on a forecasting program for my Industrial Engineering Senior
Design group. We need a function that can count in the following way.

Given the column:

1
3
4
2
0
0
2
5
0
4

This program should return

1
1
1
1
0
0
3
1
0
2

What this program is doing is looking at each cell in the column. If the
cell is zero, the function will return a 0 for the corresponding cell and
look forward to the next cell. If it is zero again, it will return a 0 and
continue in the loop until it reaches a nonzero. Upon reaching a nonzero, it
will count itself and the number of zero cells before it and return that
value. If there is a nonzero cell proceeded by a nonzero cell, the function
will simply return one.

I know this sounds crazy but we are desperate for help.

Thanks.

-
Zachary

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
How to loop saman110 via OfficeKB.com Excel Discussion (Misc queries) 4 July 25th 07 01:09 AM
DO LOOP in VBA Brettjg Excel Discussion (Misc queries) 5 April 24th 07 12:42 AM
getting out of a if loop rk0909 Excel Discussion (Misc queries) 3 April 11th 06 03:55 PM
vba loop Jackie Excel Discussion (Misc queries) 4 February 28th 06 04:20 AM
Which loop to use theguz Excel Discussion (Misc queries) 2 August 5th 05 07:01 PM


All times are GMT +1. The time now is 08:50 PM.

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"