help understanding formula

Imran Azam

Board Regular
Joined
Mar 15, 2011
Messages
103
Hi Guys

can anyone explain to me what this formula is doing?

the formula: =IF(AND($C2=1,G2<>0),0,IF(AND($C2=1,MONTH($B2)&YEAR($B2)=MONTH(H$1)&YEAR(H$1)),$F2,IF(MONTH($A2)&YEAR($A2)=MONTH(H$1)&YEAR(H$1),$F2,IF(AND(RIGHT(H$1,4)&MID(H$1,4,2)&LEFT(H$1,2)>TEXT($D2,"YYYYMMDD"),RIGHT(H$1,4)&MID(H$1,4,2)&LEFT(H$1,2)<= TEXT($E2,"YYYYMMDD")),$F2,0))))

data below
ABCDEFGH
1Start DateEND DATELENTHfirst date of Startfirst date of END per mth30/06/201631/07/2016
213/08/201512/08/20161201/08/201531/07/2016133.331466.63133

<tbody>
</tbody>

the formula is typed in column H2 ( the output is 133 )

thank you for any help provided.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
The formula returns the value in F2 if one of these conditions is TRUE

C2=1 and G2 <> 0
or
C2 =1 and the year and month of B2 match the year and month of H1
or
year and month of A2 match the year and month of H1
or
The date in H1 is greater than the date in D2 and the date in H1 is less than or equal to the date in E2

otherwise return 0
 
Last edited:
Upvote 0
Let's break it down:

Code:
IF(AND($C2=1,G2<>0),0

If LENTH is 1 and the value under 30/06/2016 is not 0 then the value is 0.

Code:
IF(AND($C2=1,MONTH($B2)&YEAR($B2)=MONTH(H$1)&YEAR(H$1)),$F2

If LENTH is 1 and the END DATE month/year is the same as H1 (July 2016) then the value is the same as F2

Code:
IF(MONTH($A2)&YEAR($A2)=MONTH(H$1)&YEAR(H$1),$F2

If the Start Date month/year is the same as H1 (July 2016) then the value is the same as F2

Code:
IF(AND(RIGHT(H$1,4)&MID(H$1,4,2)&LEFT(H$1,2)>TEXT($D2,"YYYYMMDD"),RIGHT(H$1,4)&MID(H$1,4,2)&LEFT(H$1,2)<= TEXT($E2,"YYYYMMDD")),$F2

This is a complex way to say if H1 is after first date of Start and H1 is before first date of END then the value is same as F2 - this is the condition that's met in your example. If none of the conditions above are met then the value is 0.

I assume H2 is formatted without decimal places as the actual value should be 133.33. H1 must also be formatted as text and not an actual date. It may well simplify to this:

Code:
=IF(AND($C2=1,G2<>0),0,IF(AND($C2=1,EOMONTH($B2,0)=EOMONTH(H$1,0)),$F2,IF(EOMONTH($A2,0)=EOMONTH(H$1,0),$F2,IF(AND(DATEVALUE(H$1)>$D2,DATEVALUE(H$1)<=$E2),$F2,0))))

WBD
 
Last edited:
Upvote 0
Let's break it down:

Code:
IF(AND(RIGHT(H$1,4)&MID(H$1,4,2)&LEFT(H$1,2)>TEXT($D2,"YYYYMMDD"),RIGHT(H$1,4)&MID(H$1,4,2)&LEFT(H$1,2)<= TEXT($E2,"YYYYMMDD")),$F2

This is a complex way to say if H1 is after first date of Start and H1 is before or equal to the first date of END then the value is same as F2 - this is the condition that's met in your example. If none of the conditions above are met then the value is 0.

I assume H2 is formatted without decimal places as the actual value should be 133.33. H1 must also be formatted as text and not an actual date. It may well simplify to this:

Code:
=IF(AND($C2=1,G2<>0),0,IF(AND($C2=1,EOMONTH($B2,0)=EOMONTH(H$1,0)),$F2,IF(EOMONTH($A2,0)=EOMONTH(H$1,0),$F2,IF(AND(DATEVALUE(H$1)>$D2,DATEVALUE(H$1)<=$E2),$F2,0))))

WBD

See above
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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