+ Reply to Thread
Results 1 to 24 of 24

Change range by condition

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

    Change range by condition

    Hi

    how can i change a range in a countif formula by condition

    if A5=1

    at b1 is formula =countif($B$1:B100,5)

    when formula comes at B5 i need formula change as = countif($B$5:B100,5)
    Last edited by pedersenn; 08-31-2017 at 07:47 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,091

    Re: chnage range by condition

    What is 'counting' meant to be? There is no such function. Please provide some sample data with expected outcomes.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,499

    Re: Change range by condition

    Your question is somewhat vague. for this part
    at b1 is formula =countif($B$1:B100,5)

    when formula comes at B5 i need formula change as = countif($B$5:B100,5)
    you could simply change this =countif($B$1:B100,5) to this =countif(B1:$B$100,5) then when you copy and past it in cell B5 it will automatically become =countif(B5:$B$100,5) but I don't understand the first part where you are asking if A5 = 1, what does that have to do with changing the countif formula?
    Make contributors happy, click on the "* Add Reputation" as a way to say thank you.
    Sam Capricci

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

    Re: Change range by condition

    yes i can change it manual but that must be changed many times (hundreds times)

    A5=1

    A25=1

    A50=1


    Start formula is =countif($B$1:B1,criteria)
    when formula comes at a5 or A25 etc the range must change in B$5$:B5,criteria
    $B$25:B25,criteria and =countif($B$50:B50,criteria).
    Last edited by pedersenn; 08-31-2017 at 08:45 AM.

  5. #5
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,499

    Re: Change range by condition

    You don't have to change it many times, you only have to change it once. when you have a $ sign before the column ($B for example) it locks in the column. When you have the $ before the row number (B$1) it locks in the row. $B$1 locks in the cell. So in your original formula =COUNTIF($B$1:B100,5) you are locking in cell B1 but advancing B100. So if you copy and paste the formula from A1 (or wherever it is) to A5 for example, the formula will change from this =COUNTIF($B$1:B100,5) to this =COUNTIF($B$1:B105,5) But if you want the formula to lock in B100 as the end of the count formula but advance from B1 to B5 to B25 to B50 etc, then I'm saying change it from =COUNTIF($B$1:B100,5) to =COUNTIF(B1:$B$100,5) then when you copy it to A5 or A25 it will look the way you want. [=COUNTIF(B5:$B$100,5) and [=COUNTIF(B25:$B$100,5)]. You won't have to do it manually hundreds of times. Change your original formula and then copy it to each location you want.

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

    Re: Change range by condition

    the range must be changed when comes at A5 , A25, A50 etc

    if on column C is formula =countif($B1:B5,criteria)

    when formula comes at A5 then range must be countif($B$5:B5,criteria)

    when formula comes at A25 the range must be countif($B$25:B25,criteria)

  7. #7
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,499

    Re: Change range by condition

    when formula comes at A5 then range must be countif($B$5:B5,criteria)

    when formula comes at A25 the range must be countif($B$25:B25,criteria)
    you're counting only one cell?
    apart from that, the first reference of your formula $B$5 is locking in that cell. If you change the formula to B5 without the $ every time you copy and past it to a new location it will index to that new location. So if you have in A1 the formula =COUNTIF(B1:B100,5) and you copy that and paste it into A5 it will index to become =COUNTIF(B5:B105,5) and if you paste it into A25 it will become =COUNTIF(B25:B125,5)
    if you want it to only go to B100 then writing the original formula as =COUNTIF(B1:$B$100,5) will allow you to copy it from A1 to A50 and it will change from =COUNTIF(B1:$B$100,5) to =COUNTIF(B50:$B$100,5)
    I don't know how else to explain it. If this is not what you want you may have to post a sample which represents your data and results expected.

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

    Re: Change range by condition

    see the file and paste a formula to make results like in column C
    Attached Files Attached Files

  9. #9
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,499

    Re: Change range by condition

    from what you posted I still cannot tell what you are trying to accomplish but I wrote my formula and pasted it in the column you marked as "your formula" across from each row where there was a "1" in column A to show how it "indexes" as it progresses.
    Attached Files Attached Files

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

    Re: Change range by condition

    the results must be as in column C. the formula must count result for every cell when drag cell down.
    the start range must be at same row $B$5:B5,criteria or $B$10:B10,criteria etc

  11. #11
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,499

    Re: Change range by condition

    all you have to do is copy the formula from the column where you wrote "your formula" in column D to column C then copy down like I did in the attached.
    Attached Files Attached Files

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

    Re: Change range by condition

    the formula must bigin with =countif($B$5:B5,criteria)

    the formula must be present in every cell on column E

  13. #13
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,499

    Re: Change range by condition

    if this isn't what you want then you'll need to upload a sheet with some expected results.
    BTW what you wrote in this post appears to contradict what you wrote in the first post where you wanted to index the formula.
    Attached Files Attached Files

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

    Re: Change range by condition

    i uploaded the file


    on column A are some 1's when i drag down the formula the row of
    range must change as row where is 1.
    1 is on row 10, row 15 and row 25

    so range must change from $B$5:B5,3 to $B$10:B10,3 then $B$15:B15,3 and so on
    you must find a complex formul using IF function or other functions.
    Attached Files Attached Files

  15. #15
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Change range by condition

    It appears that you need a dynamic range in first argument of COUNTIF.

    BTW. Your profile says Excel 2007. You uploaded an *.xls file. I uploaded same in case this has to be compatible with earlier version.

    The formula is in the file.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Dave

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

    Re: Change range by condition

    the formula goes wrong at E22 the result must be 2 becose are 2 of 3 number

  17. #17
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Change range by condition

    Nope.
    According to the 5 row pattern you indicate a new range "anchor" starts at row 20. At row 22 the range is now $B20:B22. The values are {4;1;3}. There is one 3.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,091

    Re: Change range by condition

    Dave - I don't think it's every five rows - I think it's each time a 1 appears in column A that the formula needs to reset. Unfortunately, I haven't managed to work out how to adapt your formula for that.

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,091

    Re: Change range by condition

    OK - you will need to add an extra 1 to cell A5 for this to work:

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    1
    cond
    Data
    formula
    Formula change
    Formula propusa
    2
    3
    4
    5
    1
    3
    1
    "=COUNTIF($B$5:B5,3)"
    1
    6
    45
    1
    "=COUNTIF($B$5:B6,3)"
    1
    7
    3
    2
    etc
    2
    8
    23
    2
    2
    9
    46
    2
    2
    10
    1
    4
    0
    "=COUNTIF($B$10:B10,3)"
    0
    11
    7
    0
    0
    12
    5
    0
    0
    13
    2
    0
    0
    14
    3
    1
    1
    15
    1
    55
    0
    "=COUNTIF($B$15:B15,3)"
    0
    16
    7
    0
    0
    17
    5
    0
    0
    18
    4
    0
    0
    19
    3
    1
    1
    20
    4
    1
    1
    21
    1
    1
    1
    22
    3
    2
    2
    23
    9
    2
    2
    24
    76
    2
    2
    Sheet: Sheet1

    Excel 2016 (Windows) 32 bit
    E
    5
    =COUNTIF(INDIRECT(ADDRESS(LOOKUP(2,1/($A$5:A5=1),ROW($A$5:$A5)),2)&":"&ADDRESS(ROW(),2)),3)
    Sheet: Sheet1

  20. #20
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,091

    Re: Change range by condition

    Here's a version that will work WITHOUT having to add an extra 1 in A5:

    =IF(COUNTIF(A$5:A5,1)<1,COUNTIF(B$5:B5,3),COUNTIF(INDIRECT(ADDRESS(LOOKUP(2,1/($A$5:A5=1),ROW($A$5:$A5)),2)&":"&ADDRESS(ROW(),2)),3))

  21. #21
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Change range by condition

    Quote Originally Posted by AliGW View Post
    Dave - I don't think it's every five rows - I think it's each time a 1 appears in column A that the formula needs to reset. Unfortunately, I haven't managed to work out how to adapt your formula for that.
    Hmmm. That could explain a lot!! I hadn't noticed that pattern. Thank you Ali.
    I solved the wrong problem. I do that a lot!
    Last edited by FlameRetired; 09-01-2017 at 03:09 PM.

  22. #22
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Change range by condition

    Quote Originally Posted by pedersenn View Post
    the range must be changed when comes at A5 , A25, A50 etc

    if on column C is formula =countif($B1:B5,criteria)

    when formula comes at A5 then range must be countif($B$5:B5,criteria)

    when formula comes at A25 the range must be countif($B$25:B25,criteria)
    This part is confusing.

    Where do you want to start the ranges? I had assumed B5. Now I am not so sure of much.

  23. #23
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,091

    Re: Change range by condition

    Thanks for the rep!

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  24. #24
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Change range by condition

    An afterthought.

    This one uses two helper columns.
    In F5:F49
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    In G5:G49
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Then in F5:F49 the counting formula.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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. [SOLVED] My vba Transpose with condition, but its transposing the condition from other range
    By Franky alta in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-28-2015, 03:56 PM
  2. [SOLVED] VBA Chnage Font Properties
    By hobbiton73 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-21-2015, 03:02 AM
  3. To chnage date to Weeknum
    By Akatecho in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-31-2015, 10:01 AM
  4. Compare two cols in different workbook and then chnage the value in third col
    By sadu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-02-2012, 02:59 PM
  5. Chnage info in 2 workbooks simultaneously
    By Frankie5d in forum Excel General
    Replies: 0
    Last Post: 07-21-2011, 02:12 PM
  6. Replies: 6
    Last Post: 05-06-2010, 10:06 PM
  7. Is there a way to chnage cell formulas to VBA module?
    By excel_2007_user in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-02-2007, 11:58 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