Count consecutive negative numbers

Godwin117

Board Regular
Joined
Dec 19, 2019
Messages
68
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking for a formula or vba, whichever is easier, where if the value in the row is negative, it will count until the next 0 or positive number, for the whole column. An example is below. I searched and couldn't find the exact thing I was looking for. Any help will be greatly appreciated.
Original DataConsecutive negative numbers
10
-21
00
-13
-12
-21
30
20
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi @Godwin117, I have tested this and it works as you want it to:
VBA Code:
Option Explicit

Sub CountCumulativeNegatives()
    Dim dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
       
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
   
    Dim cnt As Long
    cnt = rng.Rows.Count
   
    Dim con As Long
    con = 0
       
    Dim loo As Long, lo2 As Long
    For loo = 2 To cnt
        If Cells(loo, 1).Value < 0 Then
            dic.Add key:=loo, Item:=1
            If con > 0 Then
                For lo2 = con To 1 Step -1
                    dic(loo - lo2) = dic(loo - lo2) + 1
                Next lo2
            End If
            con = con + 1
        Else
            dic.Add key:=loo, Item:=0
            con = 0
        End If
    Next loo
   
    Dim key As Variant
    For Each key In dic.Keys
    '    Debug.Print key, ":", dic(key)
        Cells(key, 2) = dic(key)
    Next key
   
End Sub
It requires the data to be in column A and the output to column B, but could be easily adjusted. It also presumes the active sheet is the input.

The logic could be better - e.g. working backwards up column A would actually be easier, but I was done before I thought about doing that.
 
Upvote 0
Indeed, here's the simpler version, which you may prefer (unless you have another use for the dictionary in V1):

VBA Code:
Sub CountCumulativeNegativesV2()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
    Dim cnt As Long
    cnt = rng.Rows.Count
    Dim con As Long
    con = 0
        
    Dim loo As Long
    For loo = cnt To 2 Step -1
        If Cells(loo, 1).Value < 0 Then
            Cells(loo, 2) = Cells(loo + 1, 2).Value + 1
        Else
            Cells(loo, 2) = 0
        End If
    Next loo
    
End Sub
 
Upvote 0
Hi @Godwin117, I have tested this and it works as you want it to:
VBA Code:
Option Explicit

Sub CountCumulativeNegatives()
    Dim dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
      
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
  
    Dim cnt As Long
    cnt = rng.Rows.Count
  
    Dim con As Long
    con = 0
      
    Dim loo As Long, lo2 As Long
    For loo = 2 To cnt
        If Cells(loo, 1).Value < 0 Then
            dic.Add key:=loo, Item:=1
            If con > 0 Then
                For lo2 = con To 1 Step -1
                    dic(loo - lo2) = dic(loo - lo2) + 1
                Next lo2
            End If
            con = con + 1
        Else
            dic.Add key:=loo, Item:=0
            con = 0
        End If
    Next loo
  
    Dim key As Variant
    For Each key In dic.Keys
    '    Debug.Print key, ":", dic(key)
        Cells(key, 2) = dic(key)
    Next key
  
End Sub
It requires the data to be in column A and the output to column B, but could be easily adjusted. It also presumes the active sheet is the input.

The logic could be better - e.g. working backwards up column A would actually be easier, but I was done before I thought about doing that.
Thank you that worked perfectly.
 
Upvote 0
You said a formula would do so you could try this fairly short one, copied down.

20 05 24.xlsm
AB
1Original DataConsecutive negative numbers
210
3-21
400
5-13
6-12
7-21
830
920
Count
Cell Formulas
RangeFormula
B2:B9B2=(B3+1)*(A2<0)
 
Upvote 0
The above could be adapted to a macro if you prefer that method

VBA Code:
Sub CountNegatives()
  With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=(R[1]C+1)*(RC[-1]<0)"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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