Changing one date based on another in Excel - excel

I'm trying to create a cell A7 that changes to the date 2 days prior to date input in another cell A5.

The DateFormula does what you Need:
=DATE(YEAR(A5),MONTH(A5),DAY(A5)-2)
-- leaving this here because still true for VBA --
The DateAdd function is only available in VBA, not as a cellfunction
Put this in A7:
=DateAdd("d", -2, A5)
first Google result

Related

Can you define names for dates in Excel to use them in formulas?

I hope you are all well.
I have a question. I have a list with many payments listed on column A with dates (format day/month/year) in column B and I would like to use a formula which says something like
IF= A1 = Q2, "pay", "don't pay" , IF= A2 = Q2, "pay", "don't pay"...
Q2 means quarter 2
Could I define names for dates? For example Q2 would be 01/04/2021 - 30/06/2021 so all the dates within that range would be named Q2.
Best regards
You may try anyone of the approaches as explained below, the first one is using ROUNDUP & MONTH Function and the other one is using DEFINED NAMES.
First Approach ---> Using ROUNDUP & MONTH FUNCTION
• Formula used in cell B3
="Q"&ROUNDUP(MONTH(A3)/3,0)
• Formula used in cell C3
=IF("Q"&ROUNDUP(MONTH(A3)/3,0)="Q2","Pay","Don't Pay")
So you can see i have used two columns in the first approach just to make it understandable, therefore just wrap the formula in cell B3 within an IF logic as shown in cell C3 to get the desired output.
Second Approach --> Using DEFINED NAMES & SUMPRODUCT FUNCTION
• Formula used in Defined Names
=ROW(INDIRECT(--TEXT("01-04-2021","dd/mm/yyyy")&":"&--TEXT("30-06-2021","dd/mm/yyyy")))
So you can see I have Defined the Quarter 2 as _Q2 and the reason is a name must either begin with a letter, underscore (_), or backslash (). If a name begins with anything else, Excel will throws an error.
Therefore the formula used in cell D3
=SUMPRODUCT((A3>=_Q2)*(A3<=_Q2))
The above formula creates an array of TRUE's & FALSE's and multiplies to return the corresponding values.
Now the above formula when wrapped within an IF Function it gives us the requisite output as desired,
Formula used in cell E3 (Same it can be done in one cell, to make it understandable i have used two columns)
=IF(SUMPRODUCT((A3>=_Q2)*(A3<=_Q2))=1,"Pay","Don't Pay")
So this is how you can used a Defined names for dates in excel and then use the same within a formula.
This solution does not provide names for dates but it might meet your needs:
Make sure column A is formatted as a date, then use this formula to get the quarter from the month (this array allows you to set Q1 if it is not the same as calendar quarters):
="Q"&CHOOSE(MONTH(A1),1,1,1,2,2,2,3,3,3,4,4,4)
Then test the value of this column:
=if(C1="Q1", "Pay", "Do not Pay")
You could also create a cell at the top of your spreadsheet and name it current_quarter. Then you would type in the current quarter "Q1", "Q2", ... and your formula would be
=if(C1= current_quarter, "Pay", "Do Not Pay")
You are using standard calendar month quarters, so we can get the quarter number easily by dividing and rounding up.
=ROUNDUP(MONTH(A1)/3,0)
You can then use this number in your IF function.
=IF(ROUNDUP(MONTH(A1)/3,0)=2,"Pay","Don't Pay")

Returning Previous Quarter and Year Google Sheets

I am having trouble writing a formula in Google sheets that will return the previous quarter based on today's date.
For example, if I have cell A1 as =today() which is 1/26/22, I would like cell B1 to return the previous quarter "Q4 2021".
Thanks in advance!
I didn't hear back from you on my comment to your original post. But assuming that you are, in fact, using Google Sheets and that you are actually only needing the return for one cell (A1), this should work in B1:
=VLOOKUP(MONTH(A1),{1,"Q4";4,"Q1";7,"Q2";10,"Q3"},2,TRUE)&" "&YEAR(A1)-IF(MONTH(A1)<4,1,0)
If you were trying to get such a return for an entire columnar range (say, A2:A with a heading in A1), you could use this version in B1:
=ArrayFormula({"Prev. Quarter"; IF(A2:A="",,VLOOKUP(MONTH(A2:A),{1,"Q4";4,"Q1";7,"Q2";10,"Q3"},2,TRUE)&" "&YEAR(A2:A)-IF(MONTH(A2:A)<4,1,0))})
Here you go... just copy and paste this formula in B1:
=IFS(
A1="","",
AND(MONTH(A1)>=1,MONTH(A1)<=3),"Q4 "&YEAR(A1)-1,
AND(MONTH(A1)>=4,MONTH(A1)<=6),"Q1 "&YEAR(A1),
AND(MONTH(A1)>=7,MONTH(A1)<=9),"Q2 "&YEAR(A1),
AND(MONTH(A1)>=10,MONTH(A1)<=12),"Q3 "&YEAR(A1)
)

How could we add value + 5 to the entire column

I have an excel file
sno, date time , open , close
In the date and time colum I would like to change the time from EST or lets say add +5 hours to the entire column how could this be done
1 Day = 1, so 5 hours = 5/24, or a fraction of 1.
In a new cell, add =5/24, then copy/paste values. You should have a hard-coded 0.208333333333333
Now re-copy the hard-coded 0.208333333333333.
Select the entire date column, and paste special using the Add functionality.
Then reapply date formatting as needed.
If it's easier for you to understand, you can use =TIME(5,0,0) instead of =5/24.
Assuming your dates start from B2 cell:
• Input 05:00:00 in column E2
• In F2 input: =B2+E2
• Drag down E2:F2 range down

Excel: Check cell for date

Short story short:
I want to check the cell C21 if it contains a date. I can't use VB cause it's deactivated by GPO.
Used this from this page
D21 contains this:
=WENN(ISTZAHL(DATWERT(C21));"date";"no date")
in english
=IF(ISNUMBER(DATEVALUE(C21))...
C21 this:
=HEUTE() # in english: =TODAY() Maybe other dates later, but allways in the correct format
but it allways returns "no date"
Use this: =IF(LEFT(CELL("format",C21))="D",..,..). Learn more about CELL formula here.
In your example =TODAY() already a real date and not a date stored as text, so doesn't make too much sense to use DATEVALUE there.
Update
Here are some example dates and how CELL recognize them:
format value output
dd/mmmm/yyyy 12/June/2015 D1
dd/mm/yyyy 12/06/2015 D1
yyyy 2015 G
general 2015 G
dd/mm 12/06 D2
mmmm June G
dd mmmm yyyy 12 June 2015 G
Note: CELL is not volatile, so if the format of source cell is changed it won't be refreshed automatically, you need to either recalculate your sheet / book, either open the formula and press enter (also automatice recalculation initiated by any other volatile formula will cause it to refresh).
Use this formula, the expression will return TRUE if cell A1 contains an invalid date.
=ISERROR(DATE(DAY(A1),MONTH(A1),YEAR(A1)))
This formula works by evaluating each component part of the date: DAY, MONTH and YEAR and then aggregating them using the DATE function.
ISERROR will the catch any errors by returning TRUE (invalid) otherwise FALSE (valid).
Obviously the date value in cell (A1) must contain values >= 01/01/1900.
Useful for "Conditional Formatting".
Excel stores dates as numbers. 1 is 1-Jan-1900.
When it comes to numbers in a cell, Excel cannot tell if a number is meant to be a number or a date.
Today is 11/06/2015 as a date and 42166 as a number. For the consumer of a spreadsheet, the cell can be formatted to display the number as any number format or as a date. Excel formulas cannot tell whether the number in that cell is "meant" to be a date.
So, there is no Excel formula that you can use in a spreadsheet that will tell you if cell A1 is about 42166 widgets or if contains the date of June-11-2015.
Some of provided answers, checks cell format, which will not considers cell value, as you can format differently each cell no matter of its content, for checking if a cell value is date you can use this:
if(ISERROR(VALUE(c21)),"No Date", ----do staff for Date ----)
or shorter version which just will inform you "no date" status. If a date entry is find, it will return its numerical value instead:
IFERROR(VALUE(c21),"No Date")
hope this helps,
Cheers,
M
use the following formula ...
=IF(NOT(ISERROR(DATEVALUE(TEXT(C21,"mm/dd/yyyy")))),"valid date","invalid date")
I think it will solve your problem.
If the value is a string this method would work.
TEXT(DATE(VALUE(RIGHT(AI8,4)),VALUE(MID(TRIM(AI8),4,2)),VALUE(LEFT(AI8,2))),"dd.mm.yyyy") = AI8
Just wanted to add to the discussion that although you can check with a date(year;month;day) it will give you false positives, since the way it works is that it seems to just roll over values that exceed the logical limit, i.e. if you have the date 99-12-35 it will assume that you meant to write Jan 4th 2000, since that is the 35 - the 31 days of December. In other words, you will find some errors, but not all. Same goes for months exceeding 12.
try this formula (depends on date format):
=IF(DATE(TEXT(C21;"yyyy");TEXT(C21;"mm");TEXT(C21;"dd"));"contains date";"doesn't")
I tried every solution posted but none of them work if there are empty cells in the date cells, so I added a check to GBGOLC's answer and now it works with empty cells:
=IF(NOT(OR(ISBLANK(C21),ISERR(DATE(DAY(C21),MONTH(C21),YEAR(C21))))),"Yes it is a date","Not a date")

Excel - Sheet as a function

I have two excel sheets. The first contains a formula for calculation with one input cell (A1), and one output cell (B1). The formula for B1 could be B1 = A1 * 3 (example).
The second sheet contains various values in column A: A1 = 4; A2 = 9; A3 = 5 ... In corresponding column B of this sheet I'd like to get the result of B1 (first sheet) = A1 (second sheet) * 3 for each A (second sheet) input value.
Basically I'd like to treat the first sheet as a function, where A1 is the argument and B1 the result that is passed back to the second sheet's B column.
Sheet 2
A1 4 B1 12 (result from sheet 1)
A2 9 B2 27 (result from sheet 1)
...
Is it possible without macros?
This is built into Excel. In version 2003, use the Data, Table menu.
You can find many examples on the net. Here is one.
You can create such tables with either 1 or 2 entries (parameters).
I don't think so .....
If in B1 Sheet1 you have
3*A1
If you try this in Sheet2 B1
`=SUBSTITUTE(Sheet1!$B$1,"A1",A1)`
it will give
3*4, and Sheet2 B2 will be
3*9etc
But I don't see how you could coerce this to a numberic calculation with formulae without possibly some heavy duty formula string parsing to separate numbers from operators (which is unlikley to flex as desired if you change the entry in B1 Sheet 1)
[Update 2: but fwiw I have done it with a named range]
I used this range name
RngTest
=EVALUATE(3*INDIRECT("rc[-1]",FALSE))
This is a global range name so it will work on any sheet, more powerful than my prior OFFSET effort. It multiplies the cell to the immediate left by 3
so entering =RngTest in B1:B3 (and then in this new example C1:C3 as well)
gives the output you want
I think you want to use this in your sheet two column.
Sheet1!B1 * Sheet2!A1
Entirely without VBA: expect lots of pain, I won't go there. But...
To substantially reduce the amount of pain, you could actually use this one tiny VBA user-defined function (not technically a "macro"), basically just a wrapper to make VBA's Evaluate function available in worksheet formulas:
Function eval(myFormula As String)
eval = Application.Evaluate(myFormula)
End Function
You could then use it like this in cell B1 on sheet 2:
=eval(SUBSTITUTE(Sheet1!$B$1,"A1","A"&ROW()))
Note that this requires Sheet 1 cell B1 to contain A1*3 and not =A1*3, i.e. no equal sign. Maybe with a bit more fiddling around, it can be made to work even with the = sign there...
EDIT: Actually, if you format Sheet 1 cell B1 as Text before typing in the formula, this will work even if your formula starts with a =.
Is it possible without macros?
Yes!
You can now use =LAMBDA for this.
Define your function using the name manager, then reference it in your second sheet's formula.
See syntax at Introducing the LAMBDA function.
For more information about how to use the LAMBDA function, see the support documentation.

Resources