+ Reply to Thread
Results 1 to 5 of 5

consecutive negative numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    consecutive negative numbers

    Hi can anyone help me with some vba.

    I have a column in a spreadsheet column T that has positive and negative values, I want to count how many consecutive negative numbers before there is a positive number.

    This will happen more then once so for example 7 negative numbers before a positive number.
    Then 10 negative numbers before a positive number

    I need to write the answers in column V for example
    7
    10

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: consecutive negative numbers

    This seems like it does what you want:
    Option Explicit
    
    Sub SequentialNegatives()
    Dim NegCnt As Long, RW As Long, LR As Long
    
    For RW = 1 To Range("T" & Rows.Count).End(xlUp).Row
        If Range("T" & RW) >= 0 Then
            If NegCnt > 0 Then
                Range("V" & Rows.Count).End(xlUp).Offset(1).Value = NegCnt
                NegCnt = 0
            End If
        Else
            NegCnt = NegCnt + 1
        End If
    Next RW
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: consecutive negative numbers

    Hi thanks that works is there any way to ignore the blank cells when counting the negative numbers ?
    Last edited by Roadhouse; 02-03-2017 at 03:08 PM.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: consecutive negative numbers

    This will ignore blank cells as it counts:
    Option Explicit
    
    Sub SequentialNegatives()
    Dim NegCnt As Long, RW As Long, LR As Long
    
    For RW = 1 To Range("T" & Rows.Count).End(xlUp).Row
        If Len(Range("T" & RW)) > 0 Then
            If Range("T" & RW) >= 0 Then
                If NegCnt > 0 Then
                    Range("V" & Rows.Count).End(xlUp).Offset(1).Value = NegCnt
                    NegCnt = 0
                End If
            Else
                NegCnt = NegCnt + 1
            End If
        End If
    Next RW
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: consecutive negative numbers

    hi that's great thank you for the help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 04-14-2015, 08:34 PM
  2. Replies: 10
    Last Post: 10-27-2014, 10:17 AM
  3. Trend formula- consecutive positive and negative numbers
    By Col83 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-05-2014, 02:53 AM
  4. [SOLVED] Find consecutive 15 positive or negative numbers in a column
    By Sarangsood in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-19-2013, 05:26 AM
  5. [SOLVED] Count Consecutive Positive/Negative numbers
    By Taislin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2013, 01:02 PM
  6. Sum of time for consecutive positive/ negative numbers
    By DexterG in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-25-2012, 04:03 AM
  7. Replies: 6
    Last Post: 11-13-2012, 07:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1