Excel: Extract full months between date ranges - excel

Users are entering date ranges into an Excel spreadsheet. These date ranges span multiple months. What I'd like is for a field to display how many full months are represented in the date range.
For example:
User enters 8/25/2014 and 12/19/2014
Calculated field = 3
The full months represent September, October, and November. August and December are only partial months.

Assuming start date in A2 and end date in B2 use this formula for the number of full months
=DATEDIF(EOMONTH(A2-1,0)+1,B2+1,"m")
That will give you 3 for your example - if you change the start date to 8/1/2014 (or the end date to 12/31/2014) you will get 4 (or if you do both you'll get 5)

Related

Excel Date Formula + 3 Months

I am looking to have a formula that takes the date that is manually input in a cell (E2) and automatically populates a date in the column next to it (F2) that is + 3 months from the date manually input in (E2).
eg. If (E2) has 03/11/2021 input manually then the formula in (F2) would populate 03/02/2021. image of part of the sheet
Is this what you are looking for?
=DATE(YEAR(A1),MONTH(A1)+X,DAY(A1))
where X = 3
One can use EDATE():
=EDATE(A1,3)
The second criteria is the number of Months to add.
EDATE will go to the number of months then try to equate the day. If the day exceeds the number of days in the target month it returns the last day of the target month.
For example =EDATE(DATE(2021,11,30),3) will return the 28th of February 2021.

Automatically change excel cell value depending on current Month

Stock analysis Dashboard. I need to compare the stock from today vs the previous 6 months.
My stock data
Cell A1:R1 - month data example Cell A1= June 2017, B1= 7/2017 ....till R1= December 2018.
Cell A2:R2 Stock numbers data example A2=23, B2=25,........till R2=50.
IF I want to see the stock today month which is July 2018= M1 vs six months February 2018 H2 and the stock level calculation will which is M2=26 - H2= 30 =-4.
But instead of entering formula every time I need to be updated every month based on the today month?
Any tips on how to do it?
Thanks
I also miss a date...
If you use today() function to identify today's date you can use:
=HLOOKUP(TODAY(),$A$1:$S$2,2)-HLOOKUP(DATE(YEAR(TODAY()),MONTH(TODAY())-5,1),$A$1:$S$2,2)
If you need a cell with a specific date, replace today() formula with that cell.
I find the question a bit confusing. If A1 is June 2017, I am missing a month if R1 is December 2018 and July 2017 is in M1. Also, even though you said 6 months, I am assuming that you want 5 months so that July compares to February.
This response assumes that A1 is wrong but R1 and M1 (and B1 through R1 are accurate) and that the values in A1 through R1 are actually an Excel date that is formatted as m/yyyy (as opposed to some text representation of a date).
This formula is further complicated since I do not know what day is used in the date representation in A1 through R1, hence I am adapting both that date and the current date to be a fixed day of the month (I chose the first day of the month in my formula).
Here is the formula that computes the -4 assuming M1 is 7/2018, M2 is 26, H1 is 2/2018, H2 is 30 and we want to compare column M to column H since today is in July and we want to compare July to February (5 months difference):
=INDIRECT("R2C"&MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0),FALSE)-INDIRECT("R2C"&MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)-5,FALSE)
This is an array function so it needs to be entered with [Ctrl]+[Shift]+[Enter].
The way it works is that it computes from today's date, the date value equal to the first day of this month. Then it compares that against the range A1:R1 with each of those dates likewise moved to the first of the month. This gives us the column to use for "this month" and using the same computation but subtracting 5 gives us the column to use for "six months ago." Out of sheer laziness I used these values in an indirect cell reference to pull the Row 2 values for "this month" and "six months ago" and subtracted the latter from the former (i.e., "this month" - "six months ago").
In other words, this part of the formula gives me the column of the current month:
MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)
and this part gives me the column of the month six months ago (as defined in the original post):
MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)-5
As I indicated, this may not be what you wanted, but this is what I thought you were asking.
Alternate version using INDEX function
=INDEX(A2:R2,MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0))-INDEX(A2:R2,MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)-5)
The above would also be entered as an array formula.

Find last digits with IF

I have lots of cells in the spreadsheet that contains the whole year, date and stock prices, but I only need the dates from 25th of the last month till the 5th of this month.How can I write a function to do that? For example if the cells are like this in the column:
19900101
19900102
19900103
19900104
19900105
.
.
19900125
19900126
19900127
19900128
19900129
19900130
19900131
I want it to show only days (1,2,3,4,5,25,26,27,28,29,30,31) with the date format.
The date shows in the text format and I've tried format cell to change it but it doesn't work.
For a slightly different approach, assuming you want to return the A2 value only if it represents a date in the last 5 or first 5 days of any month you can use this formula in B2
=IF(DAY(TEXT(A2,"0000-00-00")+5)<11,A2,"")
TEXT function converts your data to a real date then if you add 5 days to that date then any date that was originally in the last 5 or first 5 of the month will now be in the first 10 days of the month.....so it's sufficient to check that the day is < 11. That will pick up 27th to 31st of a 31 day month, 26th to 30th of a 30 day month etc.
you could use a formula and then filter by rows with data...
As an example (given it's difficult to discern if you always want the first 5 and last 5 of a month or what....)
=IF(RIGHT(A2,2)*1<=2,A2,IF(RIGHT(A2,2)*1>=29,A2,""))
Based on all the edits and comments, these should be the function you need.
=IF(OR(A7-A2>5,MOD(A2,100)<=5),DATE(LEFT(A2,4),MID(A2,5,2),RIGHT(A2,2)),"")
You should place it in cell B2 and then do all the desired filtering, as explained in xQbert's answer.

Subract a series of business days from a Date

I have a given START DATE and series of milstones that happen on all business days. I need to calculate the given DATE based on subtraction of NETWORKDAYS (Business days + Holidays) from the date. Here is the data:
Start Date: (A1) 6/30/2014
Business Days to subtract:
B1-B3:
3
5
7
In a perfect world, I want to type in this function: =subtractnetworkdays (6/30/2014-B1) with the result being (6/27/2014) or if a weekend (6/25/2014).
=IF(A1,WORKDAY(A1, $B$1 * -1),"")
Note that the $B$1 is required so when you copy the formula to other cells, the same amount of business days gets subtracted. The IF statement has been added to not show #VALUE! inside the blank cells. The cell will remain blank until a date is entered.

Calculate the number of days of a date range occur between two dates

I have a datasheet that contains a list of start and end dates for a task. I need to calculate how many days between the start date and end date are part of our Christmas break (11th December to 7th January)
So for example,when start date is 10/12/2012 and end date is 12/01/2013, 28 of the days are between those dates. when the start date is 15/12/2012 and the end date is 12/03/2013, then 22 days of days are between those dates. If the start date is 10/12/2012 and the end date is 12/01/2014, 56 of the days are between those dates (because there's two years of the range).
I need to do this with a formula because of the requirements that I've been set.
Initially I decided to use the number of times Christmas Day (25th December) occurs and just calculate 4 weeks per occurrence.
The formula I used was
=FLOOR((E12-A25)/365,1)+IF(OR(MONTH(E12)=12,
MONTH(A25)=12),
IF(AND(DAY(A25)<=25,DAY(E12)>=25),1,0),
IF(OR(MONTH(A25)>=12,
IF(MONTH(E12)<MONTH(A25),
MONTH(E12)+12,
MONTH(E12))>=12),1,0))*28
But obviously this doesn't help if the range start and end date falls between those two dates.
Any suggestions? I'm at a dead end
Your date math on the second example is wrong -- there are 24 days in that range, not 22.
I can get you there for one holiday period:
LET:
A1 contain the holiday start (11-Dec-2012)
A2 contain the holiday end (7-Jan-2013)
B1 contain the start date
B2 contain the end date
FORMULA:
=MAX(MIN(A2+1,B2+1),A1) - MIN(MAX(A1,B1),A2+1)
The formula basically finds the overlapping date range, if there is one, and subtracts to get the number of whole days. The "+1" is because your "end dates" are actually inclusive, so for date math you need to have the holiday ending on 8 Jan, not 7 Jan, to capture that last day.
But this only works for a single year's holiday. You could store holiday ranges in other cells and use the same formula and add them all up, but you'll be limited to however many years you set up.
This formula will count dates between 11th December and 7th January inclusive within any date range, even across multiple years
=SUMPRODUCT((TEXT(ROW(INDIRECT(B1&":"&B2)),"mmdd")+0>=1211)+(TEXT(ROW(INDIRECT(B1&":"&B2)),"mmdd")+0<=107))
where your start date is in B1 and end date in B2
This converts every date in the range to a number , e.g. 1st Dec becomes 1201, 4th March becomes 304, then it counts those dates that are either greater or equal to 1207 (7th December) or smaller than or equal to 107 (7th January), so that will give 56 for your last example
You can shorten the formula if you subtract 7 from every date (based on 7th January as the end date) then you only need to check that resultant numbers are >= 1204, i.e.
=SUMPRODUCT((TEXT(ROW(INDIRECT(B1-7&":"&B2-7)),"mmdd")+0>=1204)+0)
.....and a third option which should also give the same result, closer to richardtallent's approach - gets the number of years and multiplies by 28 and then adjusts the figures based on the start/end date
=(YEAR(B1-7)-YEAR(B2-7)+1)*28-MAX(0,B2-DATE(YEAR(B2-7),12,11))-MIN(28,DATE(YEAR(B1-7)+1,1,7)-B1)

Resources