Date on a cell and compare it to current date - excel

Compare a date on cell vs current date, lets say on A1 and B1 having a date format DD/MM/YYYY and comparing it for today's date,
If the date on A1 is less than or equal to date today and is less than or equal to the date on B1 then it will call a sub.
The only thing I do care about is how to compare those on cell and the current date.
For example I made the cell look like this
C1 cell, has value 2021 then A1 has a formula or format I made look like this: ="2/21/"&C1 (this also includes with B1) (just for flexibility of years to come).

Related

Why date format gets changed when I insert a date value into another cell in excel by using the '=' sign?

Imagine a cell A1 has date 22-06-2022
In cell B1 I am entering this
=""""&A1&""""
I get the result as "44735" not "22-06-2022"
It is because the B1 cell doesn't have a 'Date' format, only the A1 cell has it. B1 cell only has the number value of the cell.
See Date systems in Excel (1900 system).
When you enter a date, it is converted into a serial number that represents the number of days elapsed since January 1, 1900. For example, if you enter July 5, 2011, Excel converts the date to the serial number 40729.
When you see the number 44735 as 22-06-2022 in A1, this means that the cell is formatted as DD-MM-YYYY (you can check this by selecting the cell and clicking Ctrl + 1).
So, instead of =A1 (returning: 44735 as an integer), you could use =TEXT(A1,"DD-MM-YY") to produce the same string from the value in a different cell.

Excel date increments next month based on another date

I have an overly complicated spreadsheet to show when employees have taken leave from dates in a table. It behaves differently based on if today is before or after their anniversary date. There are no macros, I'd like to keep it that way.
Currently:
Cell A1 = Today()
cell A2 = date pulled from a table, displayed as 01/01/20
I'm trying to have Cell A1 display the first of next months date if the date in cell A2 is the current month. This would push my calendar into the next years schedule. It is April, if an employee's anniversary is in April I would like A1 to show 5/1/20.
So, if A2 is not the current month, A1 shows Today(). If A2 is the current month, A1 shows the first day of the next month.
Something like this, maybe:
=IF(TEXT(TODAY(),"yyyyMM")=TEXT(A2,"yyyyMM"),DATE(YEAR(TODAY()),MONTH(TODAY())+1,1),TODAY())
Looking at the situation you present, this is I think the formula you need to use in A1:
=IF(MONTH(A2)=MONTH(TODAY()),EDATE(A2,1),TODAY())
so, we have:
If A2 month is the Current Month: MONTH(A2)=MONTH(TODAY())
Then use the next month first date: EDATE(A2,1)
Otherwise use Today's date: TODAY()
Hope this will solve your problem.

Get number of days for a month from given month and year EXCEL

Let say cell A1 has year
cell B1 has month
In cell C1 I want the number of days of month given in cell B1 in the year A1
For example: If A1=2016, B1=2, now C1 should return the number of days in February 2016.
What I have tried
In C1
=DAY(DATE(YEAR(A1),MONTH(B1)+1,))
But this does not work
I'd be grateful for a solution.
Consider:
=DATE(A1,B1+1,1)-DATE(A1,B1,1)
Or you may try this...
=DAY(EOMONTH(DATE(A1,B1,1),0))
Less transparent but this will also work
=42-DAY(DATE(A1,B1,42))
format as general
Excel have a lot of functions that are unknown for most users. EOMONTH is the function you need in your case. It returns the last date of the month for a given date.
Consider you have a cell A1 with TODAY function and you want the last date of the month. In B1 you put:
=EOMONTH("A1",0)
So in B1 will show 30/04/2017 (for those whose date format is dd/mm/yyyy).

Formula with IF AND applied to dates

I need column C to return today's date, only if column A and B both have a date in them. So if only one of the columns has a date in it then column C needs to remain blank. Column A already has an IF formula in it to populate it with today's date if another criterion is met.
Checking if a value is a date can be a bit troublesome - Excel will treat the number 1 as 1st January 1900 and will return TRUE if you ask if it's a date.
I found this formula which seems to work fairly well in identify a date:
=OR(CELL("format",A1)={"D1","D2","D3","D4","D5"})
http://www.mrexcel.com/forum/excel-questions/36619-there-%22isdate%22-type-function-excel.html
But, if your cell's formatted as a date but left blank it will still return true.
So, you need to check it's formatted as a date and contains a valid date.
=IF(AND(CELL("format",$A1)="D1",CELL("format",$B1)="D1",$A1>0,$B1>0),TODAY(),"")
This checks that both cells are formatted as d-mmm-yy or dd-mmm-yy and contains a value greater than 0. 1st January 1900 will still be accepted as a date - you may want to change the 0 to 42005 (1st January 2015) or some other value.
If you're not formatting your dates as d-mmm-yy or dd-mmm-yy you'll want to change "D1" for D2, D3, D4 or D5 - look at the help file for the CELL formula for more info.

Excel formula for just M-F and T-S of each month

I have to build a report that has two different date columns in it; M-F of the previous month, and T-S of the previous month.
If in column A I have a list of all days from the previous month
A
1 | 8/1/2013 |
2 | 8/2/2013 |
...
31 | 8/31/2013 |
Is there a formula that could be used in columns B and C to get the necessary dates?
Well, sure, you can use this formula to avoid the blank cells in between the dates:
=IFERROR(INDEX($A$1:$A$31,SMALL(IF(MOD($A$1:$A$31,7)>1,ROW($A$1:$A$31,9^99),ROW())),"")
This formula (for Monday to Friday) needs to be called with Ctrl+Shift+Enter. After doing this for the first date, you can drag down to the bottom.
For the one with Tuesday to Saturday, it's a bit longer;
=IFERROR(INDEX($A$1:$A$31,SMALL(IF(((MOD($A$1:$A$31,7)>2)+(MOD($A$1:$A$31,7)=0))>0,ROW($A$1:$A$31),9^99),ROW())),"")
But at the same time, you have to call this function with Ctrl+Shift+Enter as otherwise, you won't be able to get the other dates (you will get #NUM! instead for the other dates).
Instead of typing the days in column A, you could do something like this:
In cell B1 is this formula (formatted as m/d/yyyy):
=TODAY()
In cell B2 is this formula (formatted as mmmm):
=DATE(YEAR(B1),MONTH(B1)-1,1)
In cell A5 is this formula (formatted as m/d/yyyy):
=IF(WEEKDAY(B2,2)<6,B2,B2+1+(WEEKDAY(B2+1,2)>5))
In cell A6 and copied down to A37 is this formula (formatted as m/d/yyyy):
=IF(NETWORKDAYS(B$2,EOMONTH(B$2,0))>COUNT(A$5:A5),A5+1+2*(WEEKDAY(A5+1,2)>5),"")
In cell B5 is this formula (formatted as m/d/yyyy):
=IF(AND(WEEKDAY(A5,2)<>{1,7}),B2,B2+1+(OR(WEEKDAY(B2+1,2)={1,7})))
Lastly, in cell B6 copied down to B37 is this formula (formatted as m/d/yyyy):
=IF(B5="","",IF(MONTH(B5+1+OR(WEEKDAY(B5+1,2)={1,7})*2)<>MONTH(B$2),"",B5+1+OR(WEEKDAY(B5+1,2)={1,7})*2))
Now the M-F and T-S dates will automatically update whenever you enter a new month. If you'd prefer to manually control it, you can just type in the date of a month in cell B1, like 8/1/2013. Then the formulas will take care of the rest and will bring in the appropriate dates from July.
You can use WORKDAY function for this:
Given that you already have 1st of the previous month in A1 put this formula in B1
=WORKDAY(A$1-1,1)
.....and this one in C1
=WORKDAY(A$1-2,1)+1
then in B2....
=IF(B1="","",IF(MONTH(WORKDAY(B1,1))=MONTH(A$1),WORKDAY(B1,1),""))
and in C2
=IF(B1="","",IF(MONTH(WORKDAY(B1-1,1)+1)=MONTH(A$1),WORKDAY(B1-1,1)+1,""))
and copy those latter two formulas down to row 23
When there are fewer than 23 MF days or TS days in the month the lowest cells will display as blanks.
If you want to exclude listed holiday dates you can also do that with WORKDAY

Resources