+ Reply to Thread
Results 1 to 3 of 3

COUNTIF VBA problem

Hybrid View

  1. #1
    Registered User
    Join Date
    12-24-2013
    Location
    Eindhoven, Netherlands
    MS-Off Ver
    Excel 2013
    Posts
    1

    COUNTIF VBA problem

    Dear all,

    I want to use VBA to tackle the following problem:

    I have 2 columns, the first column has values from 1 to 3 and the last column consists of ones and zeros. Now I'm looking for a code thats works in the following manner: The code needs to return "+1" every time that it detects a value of "1" in the second column AND the value of the first column is lower than the first column value of the previously detected "1". Note that if the second column consists of all zeros and just a single value of "1" the code needs to return a value of "0". Hopefully my question becomes clear with 2 simple numerical examples:

    1 1
    2 0
    1 0
    2 0
    3 1
    1 1
    2 0
    3 1

    The code now needs to return a value of "1" since only row 5 and 6 meet the above described restriction.

    1 0
    2 1
    1 1
    2 0
    3 1
    1 0
    2 1
    3 0

    The code now needs to return a value of "2" since row 2 and 3, and row 5 and 7 meet the above described restriction.

    It would be great if anyone could help me out with this problem.

    Cheers, Raymond

  2. #2
    Valued Forum Contributor
    Join Date
    12-25-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    506

    Re: COUNTIF VBA problem

    Hi Raymond, assume the 2 columns are "A" and "B" following code will pop-up the result in message box:

    Sub test()
        Dim rng As Range
        Dim prevNum As Integer: prevNum = 1
        Dim cnt As Integer: cnt = 0
        
        For Each rng In Range("A1:A8")
            If Cells(rng.Row, "B").Value = 1 Then
                If Cells(rng.Row, "A").Value < prevNum Then
                    cnt = cnt + 1
                End If
                prevNum = Cells(rng.Row, "A").Value
            End If
        Next rng
        MsgBox cnt
    End Sub

    _______________________________________________________________________________________________________________________________
    1. Click on the * Add Reputation if you think someone helped you
    2. Mark your thread as SOLVED when question is resolved

    Alvin
    Last edited by alvin-chung; 12-27-2013 at 05:53 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    12-25-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    506

    Re: COUNTIF VBA problem

    Hi Raymond, thanks for the repu

+ 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. Excel 2007 : COUNTIF problem
    By JackHammer in forum Excel General
    Replies: 8
    Last Post: 02-03-2012, 10:47 AM
  2. COUNTIF Problem
    By Jackster in forum Excel General
    Replies: 3
    Last Post: 11-07-2006, 09:06 AM
  3. [SOLVED] Countif Problem
    By Mark W in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-21-2006, 03:40 PM
  4. COUNTIF Problem
    By Eaglered in forum Excel General
    Replies: 1
    Last Post: 05-23-2005, 02:06 PM
  5. [SOLVED] =COUNTIF Problem
    By Mike in forum Excel General
    Replies: 4
    Last Post: 04-26-2005, 06:06 PM

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