+ Reply to Thread
Results 1 to 18 of 18

vba code for count restart formula if condition

  1. #1
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    vba code for count restart formula if condition

    Hi

    does anyone want to help me with a vba code /function?

    i need a formula to restarrt count if condition is meet on a specified column on same row with condition

    A5=cond
    A8=cond


    =countif($B$1:$E1,B1)
    when drag down the formula it in
    =countif($B$1:$E2,B2)
    =countif($B$1:$E3,B3)
    =countif($B$1:$E4,B4)

    =countif($B$5:$E5,B5) the formula has changed due to the condition in A5
    =countif($B$5:$E6,B6)
    =countif($B$5:$E7,B7)
    =countif($B$8:$E8,B8) the formula has changed due to the condition in A8

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    you probably do it in some loop, so in the loop


    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    Assuming you want to have such formulas in F1:F10 and your condition is cell in column A>100

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: vba code for count restart formula if condition

    A few ways to skin this cat.
    Avoiding macros and using a dynamic range...
    I used column F as my 'change detector'. In my case, I put a 1 in the cell if 'the change occurred'. Note that the first cell in the column F range needs to be a 1.

    Next, with cell H1 selected I went to Name Manager and created this
    Name: myrng
    Formula: =OFFSET($B$1,MAX(ROW($F$1:$F1)*($F$1:$F1))-1,0,ROW()-MAX(ROW($F$1:$F1)*($F$1:$F1))+1,4)

    Then in H1:
    =countif(myrng, b1)
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  5. #5
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    can you make it a function to be insertable from formulas menu?

  6. #6
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    can you make it a function to be insertable from formulas menu?

  7. #7
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: vba code for count restart formula if condition

    @pedersenn - who are you talking to?

    @Kaper's solution is vba subroutine and is not a function
    My solution is a function that you can put in the formulas menu (it is countif, just like yours), but is using a dynamic named range.
    If you are requesting a user defined function, I'd have to wonder why, since you have two viable solutions already.

  8. #8
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    I need a function becase I do not know how to use long formulas.

    please upload a file with your example.
    Attached Files Attached Files
    Last edited by pedersenn; 09-11-2017 at 05:08 PM.

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    Attached file contains different formulas than these from post #1, so code has to be changed accordingly:
    Please Login or Register  to view this content.
    Once you run it appropriate range will be filled with formulas. delete contents of column C and run the macro.



    If what you look for is UDF (vba to be used the same way you use regular formula) here is the sample code and usage is presented in attached file:
    Please Login or Register  to view this content.
    it is written just as regular formula in C3 and copied down.
    Attached Files Attached Files
    Last edited by Kaper; 09-12-2017 at 02:55 AM.

  10. #10
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    the code works for just for a column

    why can't drag to the right?
    Attached Files Attached Files
    Last edited by pedersenn; 09-12-2017 at 12:14 PM.

  11. #11
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    i tried UDF code but i got message
    "The function take no argument"

  12. #12
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    As for "The function take no argument" - see example attached to post #9
    As no right drag - your (one column) example was used. May be you shall change some relative references to absolute or mixed.

  13. #13
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    in file you can see how results looks like.
    Attached Files Attached Files

  14. #14
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    UDF modified to work with multicolumn range:
    Please Login or Register  to view this content.
    usage (adressing to allow right-dragging) in E3:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    Thank you Mr. Kaper

    But there are some things to adjust.
    The formula works almost perfectly
    but if there are other columns with data between the column and the other data columns, it no longer works.

    Another minor problem is that when I insert the formula it displays the range incorrectly

    column A is reserved for the condition, so it should not be displayed as part of the range.

    The counting range is $B$3:C3 not $A$3:C3

    please include the option to ask for column with condition that if there are other columns with data between the
    columns numbered and the column of data make it does not alter the result.


    Thank you

  16. #16
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    First column of input_range As Range (so Column A) is used only to check condition. It is not used in "countif"
    Note:
    Please Login or Register  to view this content.
    or before (post #9)
    Please Login or Register  to view this content.
    If counting would include column A, this should be
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    If you want to use it with different data layout my suggestion is to rearrange data.

  17. #17
    Registered User
    Join Date
    04-16-2015
    Location
    Simeria
    MS-Off Ver
    2021
    Posts
    97

    Re: vba code for count restart formula if condition

    i cannot rearange data pls change code

  18. #18
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: vba code for count restart formula if condition

    It is my final approach. (again there was no attachment reflecting REAL data layout :-( I believe it is fair if you put more effort and answering person donates knowledge).

    If it works - perfect (not tested as there was no attachment with such data layout).
    If not - my suggestion is to try: https://www.excelforum.com/payments.php

    Note that it's user responsibility to use proper ranges (for instance same row number in condition_range and input_range).
    Please Login or Register  to view this content.
    sample usage:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    values in columns B:C, cond in column A
    Last edited by Kaper; 09-18-2017 at 02:32 AM.

+ 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. Formula Required to Count Rows only if condition is meet or else dont count
    By Shahbazk in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-22-2017, 08:39 AM
  2. [SOLVED] Restart count everytime value in adjacent column changes
    By Niklasie in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 04-06-2017, 07:24 AM
  3. Restart a Count within a Column
    By cookthebooks80 in forum Excel General
    Replies: 3
    Last Post: 07-06-2014, 11:46 AM
  4. Code to count under certain condition
    By Marceltcm in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-29-2013, 03:17 PM
  5. [SOLVED] Restart Line Count if Value Changes in Seperate Cell
    By jerry.fein in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-21-2013, 03:52 PM
  6. count sessions then restart 365 days after - help
    By itsnotfair in forum Excel General
    Replies: 2
    Last Post: 06-29-2011, 09:31 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