IF OR AND statement in one formula

Simmo1313

New Member
Joined
May 4, 2015
Messages
39
Hi all,

Having a bit of difficulty here figuring out where I am going wrong with this one. The table below is for a lighting audit. The inputs are in red and the formula in cells J2:AG4 are for monday only. The data set continues AH and onward for each day of the week.

I want to be able to input a "1" in the days Monday to Sunday where the lights are on and I want to put the time which they turn on in column H and when they turn off in column "I"

The formulas in the table J2:AG4 should insert a value of "1" if the light is on at that time.

The formula I have at the moment can do that only if the start time is earlier in the day than the finish time so I'm not sure where my formula is incorrect


Example formula for what I have at the moment is in J2 <i2,a2=1,j1><i2),and(h2>


<i2)),1,0))<i2,b2=1,j1><i2),(and(h2><h2,k2><i2,a2=1,j1><i2),(and(h2><h2,j1>=IF ( OR ( AND ( H2 < I2 , A2 = 1 , J1 > H2 , J1 < I2 ) , ( AND ( H2 > I2 , A2 = 1 , J1 < H2 , J1 > I2 ) ) ) , 1 , 0 )

Sorry, had to put the spaces in so that the post would leave my formula as is.

I'm hoping I should return results displayed below once the formula is correct.



ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
1MTWThFSaSuONOFF12am1am2am3am4am5am6am7am8am9am10am11am12pm1pm2pm3pm4pm5pm6pm7pm8pm9pm10pm11pm
211111



7pm7am11111111111
31

11111

5am9pm111111111111111
411111117am8pm111111111111

<tbody>
</tbody>


Any help would be appreciated.</h2,j1></i2),(and(h2></i2,a2=1,j1></h2,k2></i2),(and(h2></i2)),1,0))<i2,b2=1,j1></i2),and(h2></i2,a2=1,j1>
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this in cell J2 (you can then copy it down and across)

Code:
=IF($B2=1,IF($H2<$I2,IF(AND(J$1>$H2,J$1<$I2),1,""),IF($I2<$H2,IF(OR(J$1>$H2,J$1<$I2),1,""),"Error")),"Day not 1")

Let us know if it falls short anywhere.

Ive built in some error checking, if the result is: "Day not 1", this means the day is blank, or does not have a 1 in it.
You would need to change the B2 reference to C2 for tuesday. D2 for wednesday etc.

The other error code is "error" this means either time is entered wrong or the same on and of time is used.

What do you want to show if On = 08:00 and off = 08:00?
 
Upvote 0
Hi all,

Having a bit of difficulty here figuring out where I am going wrong with this one. The table below is for a lighting audit. The inputs are in red and the formula in cells J2:AG4 are for monday only. The data set continues AH and onward for each day of the week.

I want to be able to input a "1" in the days Monday to Sunday where the lights are on and I want to put the time which they turn on in column H and when they turn off in column "I"

The formulas in the table J2:AG4 should insert a value of "1" if the light is on at that time.

The formula I have at the moment can do that only if the start time is earlier in the day than the finish time so I'm not sure where my formula is incorrect


Example formula for what I have at the moment is in J2 <i2,a2=1,j1><i2),and(h2>


<i2)),1,0))<i2,b2=1,j1><i2),(and(h2><h2,k2><i2,a2=1,j1><i2),(and(h2><h2,j1>=IF ( OR ( AND ( H2 < I2 , A2 = 1 , J1 > H2 , J1 < I2 ) , ( AND ( H2 > I2 , A2 = 1 , J1 < H2 , J1 > I2 ) ) ) , 1 , 0 )

Sorry, had to put the spaces in so that the post would leave my formula as is.

I'm hoping I should return results displayed below once the formula is correct.



ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
1MTWThFSaSuONOFF12am1am2am3am4am5am6am7am8am9am10am11am12pm1pm2pm3pm4pm5pm6pm7pm8pm9pm10pm11pm
211111



7pm7am11111111111
31

11111

5am9pm111111111111111
411111117am8pm111111111111

<tbody>
</tbody>


Any help would be appreciated.</h2,j1></i2),(and(h2></i2,a2=1,j1></h2,k2></i2),(and(h2></i2)),1,0))<i2,b2=1,j1></i2),and(h2></i2,a2=1,j1>

Simmo1313,
Both portions of your formula evaluate to '1' for the tabled values...
IF(OR(H2 < I2 , A2 = 1 , J1 > H2 , J1 < I2 ), 1, 0 ) = 1
IF(OR(H2 > I2, A2 = 1, J1 < H2, J1 > I20), 1, 0 ) = 1

I tried various combinations of 'IF(OR(AND...' and 'IF(AND(OR...'

For the values shown in your table, the formula below looked like the most promising.
Check the logic and see if this format works for you. Excuse the extra spaces.

=IF ( AND ( OR ( H2 < I2 , A2 = 1, J1 > H2, J1< I2) ,(OR (H2 > I2, A2=1,J1 <H2, J1 > I2) ) ), 1, 0 )

Perpa
 
Upvote 0
I tried this formula Perpa and it calculates to 1 regardless of the time conditions.

at the end of your formula it shows J1 I2) is there an argument missing there i should know about?
 
Upvote 0
Try this in cell J2 (you can then copy it down and across)

Code:
=IF($B2=1,IF($H2<$I2,IF(AND(J$1>$H2,J$1<$I2),1,""),IF($I2<$H2,IF(OR(J$1>$H2,J$1<$I2),1,""),"Error")),"Day not 1")

Let us know if it falls short anywhere.

Ive built in some error checking, if the result is: "Day not 1", this means the day is blank, or does not have a 1 in it.
You would need to change the B2 reference to C2 for tuesday. D2 for wednesday etc.

The other error code is "error" this means either time is entered wrong or the same on and of time is used.

What do you want to show if On = 08:00 and off = 08:00?

Thanks CROY1985

When Input this formula it enters a value of 1 in every column except the one before the "on" time
 
Upvote 0
Thanks CROY1985

When Input this formula it enters a value of 1 in every column except the one before the "on" time

For example when I input time on at 7pm and off at 7am it inputs a value of 1 in every column except the time on time.
 
Upvote 0
I tried this formula Perpa and it calculates to 1 regardless of the time conditions.

at the end of your formula it shows J1 I2) is there an argument missing there i should know about?

Simmo1313,
Sorry for that, as you said it was difficult to enter the formula and have it read correctly.
Here is the formula with extra spaces...

IF ( AND ( OR ( H2 < I2 , A2 = 1 , J1 > H2 , J1 < I2 ) , ( OR ( H2 > I2 , A2 = 1 , J1 < H2 , J1 > I2 ) ) ) , 1 , 0 )

Perpa
 
Upvote 0
Simmo1313,
Sorry for that, as you said it was difficult to enter the formula and have it read correctly.
Here is the formula with extra spaces...

IF ( AND ( OR ( H2 < I2 , A2 = 1 , J1 > H2 , J1 < I2 ) , ( OR ( H2 > I2 , A2 = 1 , J1 < H2 , J1 > I2 ) ) ) , 1 , 0 )

Perpa

I still get that returning a value of 1 in every column.
 
Upvote 0
I still get that returning a value of 1 in every column.


Simmo1313,
There is something you may not be aware that is going on with your times, ie. '7pm, 2am'.. & etc.
Excel doesn't see those as 'times', Excel thinks they are 'text'. Try this in two cells below your work
range...Enter '7am' in one cell, then enter '7 am' (notice the space between the '7' and 'am' in the
second cell. Excel immediately converts '7 am' to '7:00 AM'. Notice also that the first cell contents
stay to the left of the cell (because it is text), whereas the second cells contents shift to the right
(because it is numbers/time). I have encountered this before and it took me a little while to
remember what I did that time. The other thing that complicates times is at midnight and noon.
Midnight is '12:00 AM' whereas noon is '12:00 PM'. For your lighting audit, you might find it easier
to give your times a 24-hour format, ie. '1:00 PM' becomes '13:00' and on.

Yesterday was a travel day for me so I wasn't able to work on this. I'll have another look at it with
the changes I have described above, and get back to you if/when I come up with something that will
work.
Perpa
 
Upvote 0
Simmo1313,
There is something you may not be aware that is going on with your times, ie. '7pm, 2am'.. & etc.
Excel doesn't see those as 'times', Excel thinks they are 'text'. Try this in two cells below your work
range...Enter '7am' in one cell, then enter '7 am' (notice the space between the '7' and 'am' in the
second cell. Excel immediately converts '7 am' to '7:00 AM'. Notice also that the first cell contents
stay to the left of the cell (because it is text), whereas the second cells contents shift to the right
(because it is numbers/time). I have encountered this before and it took me a little while to
remember what I did that time. The other thing that complicates times is at midnight and noon.
Midnight is '12:00 AM' whereas noon is '12:00 PM'. For your lighting audit, you might find it easier
to give your times a 24-hour format, ie. '1:00 PM' becomes '13:00' and on.

Yesterday was a travel day for me so I wasn't able to work on this. I'll have another look at it with
the changes I have described above, and get back to you if/when I come up with something that will
work.
Perpa

I don't think that is the reason because I have actually entered 00:00, 01:00, 02:00 through to 23:00 which excel does recognise as times because it displays 12 am ,1 am into the cell.

I got it to work doing the following.

I inserted 3 columns above the date and time picker like below

ABCDEFGHIJ
1MTWTHFRSASUT/ONT/OFF0:00
2IF ( AND ( A6 = 1 , H6 < I6 , J1 > H6 , J1 < I6 ) , 1 , 0)
3IF ( AND ( A6 = 1 , H6 > I6 , J1 > H6 ) , 1 , 0 )
4IF ( AND ( A6 = 1 , H6 > I6 , J1 < I6 ) , 1 , 0 )
5111117 PM7AMSUM ( J3 : J5 )

<tbody>
</tbody>

<tbody>
</tbody>
I then hide rows 3 , 4 and 5 and copy the formula through for all the dates.

I think the issue was I was trying to have 2 formulas in one cell which contradicted the results of each other.

If anyone has a neater solution then I am all ears....
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,824
Members
449,470
Latest member
Subhash Chand

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top