+ Reply to Thread
Results 1 to 15 of 15

IF & AND formula with cell ranges

Hybrid View

  1. #1
    Registered User
    Join Date
    09-02-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    7

    IF & AND formula with cell ranges

    Hi

    I am currently using the following formula in cell B1, which does what I want it to do if I am only looking at one cell:

    =IF(AND(9<A1,A1<13),0.5,"0.25")

    Basically, this gives me the answer 0.25 in cell B1 if the number in cell A1 is less than 9, or gives me 0.5 if the number in the cell is between 9 and 13. So good so far.

    The problem I have encounterd is where I want the formula to consider a range of cells instead of just one. For example, I would like the formula to consider what is in cells A1:A10 and if any of the cells is less than 9 then 0.25 would be added into cell B1 or 0.5 would be added if any thing was between 9 and 13, which may result in B1 containing a number such as 1.25 or 4.75. As a result, B1 would contain a running total of 0.25 and 0.5 in every instance where the criteria is met. Does that make sense? I hope so. Please help!

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,825

    Re: IF & AND formula with cell ranges

    Well, I'm surprised that the formula gives you what you want, as you are returning "0.25" and the quotes means that you have a text value rather than the number 0.25. Besides, you would get "0.25" if A1 is above 13.

    Anyway, you could try this array* formula in B1:

    Formula: copy to clipboard
    =SUM(IF((A1:A10>9)*(A1:A10<13),0.5,0.25))


    *An array formula needs to be committed using the key combination of Ctrl-Shift-Enter (CSE), rather than the usual <Enter>.

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    09-02-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: IF & AND formula with cell ranges

    Quote Originally Posted by Pete_UK View Post
    Well, I'm surprised that the formula gives you what you want, as you are returning "0.25" and the quotes means that you have a text value rather than the number 0.25. Besides, you would get "0.25" if A1 is above 13.

    Anyway, you could try this array* formula in B1:

    Formula: copy to clipboard
    =SUM(IF((A1:A10>9)*(A1:A10<13),0.5,0.25))


    *An array formula needs to be committed using the key combination of Ctrl-Shift-Enter (CSE), rather than the usual <Enter>.

    Hope this helps.

    Pete


    Thanks for your response Pete, you're right though, the more I test my original formula the more I realise it doesn't work (plus I just realised my answers for the formula to give were wrong)! What a mess!

    To explain what I am trying to do, the numbers in column A represent the hours worked in one shift. Where the shift worked is longer than 8 hours, I need to deduct 0.5 hours, but where the shift is 8 hours or less I need to deduct only 0.25 hours. How might I do that!? I need a number value returned once the test has been run on each cell, with the number being added to the previous result. So if the range was A1:A3 where A1 = 12, A2 = 3 and A3 = 10 the answer in B1 should be 1.25 (0.5 for A1, 0.25 for A2 and 0.5 for A3)

  4. #4
    Registered User
    Join Date
    12-15-2012
    Location
    Surat, India
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: IF & AND formula with cell ranges

    Hi

    Try this

    In Cell B1

    =IF(AND(9<A1,A1<13),0.5,0.25)
    Drag the Values / Copy from Cell B1 and paste from B2 to B10

    In Cell B2
    =IF(AND(9<B1,B1<13),0.5,0.25)
    Likewise in Cell B3 to B10

    In Cell C1
     =Sum(B1:B10)
    This should solve your problem.
    Last edited by uselessnut; 09-02-2013 at 10:02 AM.

  5. #5
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: IF & AND formula with cell ranges

    Maybe this regular formula?
    Formula: copy to clipboard
    =SUMPRODUCT((A1:A10<>"")*((A1:A10>=9)*(A1:A10<=13)*0.25+0.25))


    or this
    Formula: copy to clipboard
    =SUMPRODUCT((A1:A10>=9)*(A1:A10<=13)*0.25+0.25)



    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  6. #6
    Registered User
    Join Date
    09-02-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: IF & AND formula with cell ranges

    Thanks for your response Pete, you're right though, the more I test my original formula the more I realise it doesn't work (plus I just realised my answers for the formula to give were wrong)! What a mess!

    To explain what I am trying to do, the numbers in column A represent the hours worked in one shift. Where the shift worked is longer than 8 hours, I need to deduct 0.5 hours, but where the shift is 8 hours or less I need to deduct only 0.25 hours. How might I do that!? I need a number value returned once the test has been run on each cell, with the number being added to the previous result. So if the range was A1:A3 where A1 = 12, A2 = 3 and A3 = 10 the answer in B1 should be 1.25 (0.5 for A1, 0.25 for A2 and 0.5 for A3)

  7. #7
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: IF & AND formula with cell ranges

    This regular formula begins the sum of "adjusted hours"
    Formula: copy to clipboard
    B1: =A1-(A1>0)*LOOKUP(A1,{0,9},{0.25,0.5})


    and this regular formula, copied down, continues the sum
    Formula: copy to clipboard
    B2: =B1+A2-(A2>0)*LOOKUP(A2,{0,9},{0.25,0.5})


    With A1:A3 containing these values
    9
    8
    8

    These would be the returned values:
    8.50
    16.25
    24.00
    Is that something you can work with?

  8. #8
    Registered User
    Join Date
    09-02-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: IF & AND formula with cell ranges

    Quote Originally Posted by Ron Coderre View Post
    This regular formula begins the sum of "adjusted hours"
    Formula: copy to clipboard
    B1: =A1-(A1>0)*LOOKUP(A1,{0,9},{0.25,0.5})


    and this regular formula, copied down, continues the sum
    Formula: copy to clipboard
    B2: =B1+A2-(A2>0)*LOOKUP(A2,{0,9},{0.25,0.5})


    With A1:A3 containing these values
    9
    8
    8

    These would be the returned values:
    8.50
    16.25
    24.00
    Is that something you can work with?
    Thanks for trying to help me work this out! Whilst I see what your formula does, it wouldn't be very practical for my spreadsheet. The formula needs to be applied to a huge cell range therefore I want to try to avoid having duplicated cells with the "adjusted hours" in them. Ideally, I would like the formula to look at the huge cell range and provide the answer in one cell as a single figure (which will be made up of lots of instances of 0.5 and 0.25 added together).

  9. #9
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: IF & AND formula with cell ranges

    Thanks for the clarification...
    This regular formula sums the adjusted hours for the referenced range:
    Formula: copy to clipboard
    B1: =SUMPRODUCT(A1:A10-(A1:A10>0)*LOOKUP(A1:A10,{0,9},{0.25,0.5}))


    Is that something you can work with?

  10. #10
    Registered User
    Join Date
    09-02-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: IF & AND formula with cell ranges

    Quote Originally Posted by Ron Coderre View Post
    Thanks for the clarification...
    This regular formula sums the adjusted hours for the referenced range:
    Formula: copy to clipboard
    B1: =SUMPRODUCT(A1:A10-(A1:A10>0)*LOOKUP(A1:A10,{0,9},{0.25,0.5}))


    Is that something you can work with?
    Ron - you are a genius! That does work, but instead of the answer being the total of the hours as adjusted, how do I alter the formula so that the answer is the total of the adjustments to be deducted from the hours?

  11. #11
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: IF & AND formula with cell ranges

    @Hi sarah, try to click the button "calc" I added to the enclosed file.
    HIH

    Dear seniors here, is it wrong to combine in my replies here vba code?
    Attached Files Attached Files
    Last edited by GIS2013; 09-02-2013 at 10:22 AM.

  12. #12
    Registered User
    Join Date
    09-02-2013
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: IF & AND formula with cell ranges

    Quote Originally Posted by GIS2013 View Post
    @Hi sarah, try to click the button "calc" I added to the enclosed file.
    HIH

    Dear seniors here, is it wrong to combine in my replies here vba code?
    Unfortunately that doesn't seem to work...

  13. #13
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: IF & AND formula with cell ranges

    Here is the code, oops I named the sheet "calc" it must be "sheet1" in your case, isn't it?
    update - I've checked it and it works fine in my computer.. Do you see the button called "calc"?
    Worksheets("calc").Activate
    
    Set Rng = ActiveSheet.Range("A:A")
    
    lr = ActiveSheet.UsedRange.Rows.Count
    
    For i = 1 To lr
    
    If Cells(i, "A").Value > 9 And Cells(i, "A") < 13 Then
    
    ActiveSheet.Cells(i, "C").Value = 0.5
    
    Else: ActiveSheet.Cells(i, "C").Value = 0.25
    
    End If
    
    Next
    
    Range("b1").Value = Application.Sum(Range(Cells(1, 3), Cells(lr, 3)))
    
    End Sub
    Last edited by GIS2013; 09-02-2013 at 10:29 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. fixing cell/ formula ranges
    By luke_p in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-17-2009, 09:51 AM
  2. how do i add different cell ranges using a formula
    By Norman K via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-22-2005, 06:05 AM
  3. [SOLVED] formula with changing cell ranges
    By Grant in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 PM
  4. [SOLVED] formula with changing cell ranges
    By Grant in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  5. formula with changing cell ranges
    By Grant in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 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