Count instances of positive or negative numbers in a row

Eugenius

New Member
Joined
Mar 2, 2017
Messages
3
Hello this is my first post here. Hopefully I can be clear with my query so you all you excel genii might help.

Here is a sequence of numbers in a column

100
20
-78
5
-99
-54
-34
6
0
34
55
89
32
-3
-78

Now I need to COUNT the number of instances of the following data set. The answer is in brackets:

1 positive number on its own (2)
2 positive values in a row (1)
3 positive values in a row (0)
4 positive values in a row (1)

1 0 value on its own (1)
2 0 values in a row (0)
3 0 values in a row (0)
4 0 values in a row (0)

1 negative number on its own (1)
2 negative values in a row (1)
3 negative values in a row (1)
4 negative values in a row (0)

Any ideas on excel formula very welcome!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Welcome to the board!

I'd do this in steps:

1) With your data in A1:A15, set B1 =SIGN(A1) and copy that down through B15. That column will have a 1 for positive values, a 0 for zeroes, and a -1 for negative values.

2) Enter the value 1 in cell C1. Set C2 =IF(B2<>B1,1,C1+1) and copy down through C15. This will tell you how many consecutive similarly signed values you have up to the current row. For example, a 2 means that this cell and the cell above it have the same sign. A 1 means the cell above it has a different sign.

3) Set D1 =IF(C2=1,C1,IF(C2="",C1,"")) and copy down through D15. This takes the values in column C and makes them blank if they're not the last value in the current sequence.

4) Use =SUMPRODUCT(--($B$1:$B$15=1),--($D$1:$D$15=4)) to count the number of 4 positive values in a row. You'd copy the formula and change the "4" to be the 1,2,3,4 values in a row, and you'd change the "=1" to be =1 for positive values, =0 for zeroes, and =-1 for negatives.
 
Upvote 0
Wow thanks Oaktree that certainly does it...no wonder I couldn't figure it out!

Now I've got work out whether it's a practical approach to formatting thousands of lines of data. Hopefully it will be.

All the best

Eugenius





Welcome to the board!

I'd do this in steps:

1) With your data in A1:A15, set B1 =SIGN(A1) and copy that down through B15. That column will have a 1 for positive values, a 0 for zeroes, and a -1 for negative values.

2) Enter the value 1 in cell C1. Set C2 =IF(B2<>B1,1,C1+1) and copy down through C15. This will tell you how many consecutive similarly signed values you have up to the current row. For example, a 2 means that this cell and the cell above it have the same sign. A 1 means the cell above it has a different sign.

3) Set D1 =IF(C2=1,C1,IF(C2="",C1,"")) and copy down through D15. This takes the values in column C and makes them blank if they're not the last value in the current sequence.

4) Use =SUMPRODUCT(--($B$1:$B$15=1),--($D$1:$D$15=4)) to count the number of 4 positive values in a row. You'd copy the formula and change the "4" to be the 1,2,3,4 values in a row, and you'd change the "=1" to be =1 for positive values, =0 for zeroes, and =-1 for negatives.
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,705
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top