Find a previous date on a list closest to a specified date - vba

I would like to have VBA code or formula that will take a variable date value and find it on a list range of dates. If it can't find the date I want it to find the closest previous date value. VLOOKUP works great for finding an exact match but I am struggling with the code to find the closest previous date.
My only thought is to create a loop where if it doesn't match it continues to subtract a day from the variable date until it can locate a match on the list. That does not seem like the best or most efficient method and I hope someone can recommend a simpler solution.
Example:
Variable Date = 01/26/2014
Date List
02/04/2014
01/27/2014
01/24/2014
01/13/2014
12/29/2013
Desired Result = 01/24/2014
Any help would be appreciated.

Suppose your list of dates was in cells A1:A5 and your check date was in cell C1, you could enter this function ENTERED AS AN ARRAY FORMULA:
=MAX(($A$1:$A$5<=C1)*A1:A5)
Rememeber, to enter it as an array formula, hit Ctrl + Shift + Enter when entering the formula.
Hope this does the trick!!

I went about this a little differently, no arrays needed
Find how many numbers are bigger then the one you are looking for with CountIf()
Then I used =Large this will find the nth dates in a list we have the nth we are looking for in the countIF()
=LARGE(A:A,COUNTIF(A:A,">="&TODAY()))

Vlookup can do this actually, if you set the final argument to true, which looks for an approximate match. You need your dates sorted from Oldest to newest and it will return the first date that is not after your search term.

Related

Apple Numbers sum if is date is in Month

In Apple numbers I tried to sum cells if the date is in a specific month.
So I have a column A with a date and column B has a price.
Date
Price
29-07-2021
12,50
16-06-2021
15,00
I use the function sum.if and the function month that returns the number of the month.
My formule look like this
som.if(month(Date));7;Price);
But this generates an error numbers can only check one date and not for each row for counting.
Is there anybody who can help me with this?
Thanks a lot!
Use SUMPRODUCT() instead. I am using US-EN version of excel. So, decimal separator and formula definition may differ.
=SUMPRODUCT((B2:B3)*(MONTH(A2:A3)=7))
If you want to use SUNIFS() then can try-
=SUMIFS(B2:B3,A2:A3,">="&DATE(2021,7,1),A2:A3,"<="&DATE(2021,7,31))
If you have Excel-365 then can use FILTER() function like-
=SUM(FILTER(B2:B3,MONTH(A2:A3)=7))

I need to convert total date to month only

I set up a google forms form for my work where my employees pass information and this information is recorded in a spreadsheet. The information, when recorded, automatically inserts a date and time in the first column of form responses. However, when I enter the code = month (a1), it always returns the answer "1" or "January" and this information does not match the date entered in the column. How do I fix this?
If you are entering '=month(a1)' for every row, then you are always taking the month of the top left cell in the sheet. You would need to adjust the row number for the row you are in.
You could use something like '=month(now())' to ensure you are always getting the month of the current date.

Get number of days between 2 dates of this format in Excel

I have 2 dates value in Excel. They are 20170124;202000 (24Jan2017 2020hrs 00secs) and 20170515;021146 (15May2017 0211hrs 46secs). I want to get the number of days between these 2 dates.
I tried to use the formula =DATEDIF(G838,G839,"md") where the first 2 parameters are cells containing the dates. The value returned was #VALUE!.
What Excel formula or VBA function can I use to get the number of days between the 2 dates?
Try this.......
=DATEDIF(DATE(LEFT(G838,4),MID(G838,5,2),MID(G838,7,2)),DATE(LEFT(G839,4),MID(G839,5,2),MID(G839,7,2)),"d")
If you have the string in cell A1, use the formula to convert it to an actual date before finding the difference,
=DATE(LEFT(A1,4),MID(A1,5,2),MID(A1,7,2))
You can include this directly in your formula too.

Conditional mean calculation in excel

I have a dataset organized as following :
The column A is the name
The column B is the date
The column C is the value registered for that person in that day
How can i calculate for the whole dataset a mean of the value of that person in the 30 past days without manually ordering for name and making the mean checking the date?
Try the AVERAGEIFS function with the EDATE function giving you a one month window.
=AVERAGEIFS(C:C, A:A, "Jack", B:B, ">"&EDATE(TODAY(), -1), B:B, "<="&TODAY())
    
You can use a nested array formula (Ctrl+Shift+Enter instead of Enter):
=AVERAGE(IF($A$2:$A$15=A2,IF($B$2:$B$15>=TODAY()-30,$C$2:$C$15,""),""))
Add columns for these two fomulas:
=COUNTIF(A1:A4,"Jack")
=SUMIF(A1:A4,"Jack",C10:C13)
That will give you the count, and it will give you the sum. With those two you can calculate the mean.
That's the basic idea, anyway.
Of course, you can add another count for the date ranges. It's all the same sort of thing.

If particular cell is NOT BLANK copy down FORMULA from above cell

Basically I have the following formulas:
Column J: =IF(ISNA(VLOOKUP(I2,CCG,1,FALSE)),"Out of Area",VLOOKUP(I2,CCG,1,FALSE))
Column K: =INDEX(ResponsibleAgency,MATCH(N3,LeftLookup,0),MATCH(J3,TopLookup,0))
Column L: =IF(ISNA(VLOOKUP(N3,PPLookup,2,FALSE)),"Missing F Code",VLOOKUP(N3,PPLookup,2,FALSE))
Column M: =IF(ISNA(VLOOKUP(N3,PPLookup,3,FALSE))," ",VLOOKUP(N3,PPLookup,3,FALSE))
Basically, I only want these formulas to activiate provided that column A is populated with a date. If there is no date in column A, I want the cells to remain blank.
Is this possible?
Technically speaking, it is not possible to tell if cell contains a date or not in strict sense.
In Excel dates are in fact numbers representing number of days since January 1, 1900. I.e. today is 42066. It's the formatting that makes those numbers look like dates to user. If you pass a cell containing a date to any formula, it receives this number.
As a workaround, you can check if the cell satisfies two conditions:
It is a number.
It falls into some date range that makes sense.
For example, if your column contains goods delivery dates you usually don't expect them be previous century or 50 years into the future.
So you can wrap your formulae into something like this:
=IF(ISNUMBER(A1),IF(AND(A1>=DATE(2014,1,1),A1<=DATE(2015,12,13)),[LOGIC_IF_CORRECT_DATE_HERE],""),"")
Being said, it's still a workaround.