Changing String input data into week / month format - string

I'm not sure if this is possible but I thought I would ask to see if any one may have a solution.
Issue:
I have a macro were the user inputs a date as a string (e.g. 021513) in an input box. It is in string format as it's used to open a file (string at the end of the file name).
From this string (e.g. 021513) I need to derive the following in two separate fields:
Week period (e.g. Week: 11th - 15th).
Month (e.g. February).
Any help or thoughts would be greatly appreciated.
Ciaran

You need DateSerial VBA function - it will return the actual Date value from the parts of your string using Mid and other text functions. Having actual date you'll be able to return e.g. month using Month function.
Perhaps string parts conversion to numbers using CInt will be required as well.

You can also do this with Excel formulas:
Assuming your string is cell A1, use the following formulas:
B1 (date): =DATE(right(A1,2)+2000,LEFT(A1,2),MID(A1,3,2))
C1 weekday: =WEEKDAY(B1,2) - this will return 1 for Monday, 2 for Tuesday, etc.
D1 Start of the week: =B1-C1+1
E1 End of the week: =D1+4
F1 Month: =TEXT(B1,"MMMM")

Related

Identifying date in text file While date is incorrectly formatted

I need to extract the date from C2 and find the difference between the date in c2 and A1
The date is formatted as "Jul, 18 2015", any ideas? EDIT the database has a different amount of text per cell. Is there away around this so that i can apply the formula to every cell and pull the day/month/year?
you are going to need to go through a series of string manipulation and date time functions. Lets start by assuming your string is in the C2 cell. In order to do this we are going to work from the largest unit (years) to the smallest unit (days). You can do it in any order as it will all be lumped into once formula, but for the breakdown of steps its good to have an order.
Step 1) PULL OUT THE YEAR
=MID(C2,FIND(" ",C2,FIND(" ",C2)+1)+1,4)
That will give us the last 4 characters of the string after the second space which in this case is the year.
Step 2) PULL OUT THE MONTH
=MONTH(DATEVALUE(MID(C2,2,3)&"-"&1))
that looks at second character and pulls out the string 3 character long which is your month. It then converts it to a format that excel tends to recognize as a date short form by adding a - and the digit 1 to it. So in your case if would look like Jul-1. Datevalue converts this to an excel date serial, which we then pull back and grab the month from and in your case that is 7
If the above formula does not work for you it could be due to regional settings. If that is the case you can use the following:
=MATCH(MID(C2,2,3),{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"},0)
If you use this alternat formula , be sure to adjust the final equation accordingly.
Step 3) PULL OUT THE DAY
=TRIM(MID(C2,FIND(" ",C2)+1,2))
So the above formula finds the first space and then starts pulling then next 2 characters after it. Now since I do not know if the first of the month is 01 or just 1, it may wind up grabbing the space after the 1. The trim function removes excess spaces.
Step 4) BUILD THE DATE
The DATE function in excel requires the YEAR, MONTH, and DAY and converts those values in to the excel date serial. In this case we will convert:
=DATE(year,month,day)
to the following by substituting our equations from above:
=DATE(MID(C2,FIND(" ",C2,FIND(" ",C2)+1)+1,4),MONTH(DATEVALUE(MID(C2,2,3)&"-"&1)),TRIM(MID(C2,FIND(" ",C2)+1,2)))
The final touch is to ensure your cell is formatted as date and not General or some other format which will result as the date being displayed in an integer format.
Now assuming you date in A1 is in Excel format, you would simply add A1- to the front of the last formula to give you:
=A1-DATE(MID(C2,FIND(" ",C2,FIND(" ",C2)+1)+1,4),MONTH(DATEVALUE(MID(C2,2,3)&"-"&1)),TRIM(MID(C2,FIND(" ",C2)+1,2)))
Now, if A1 is also in quotes like the C2 formula, repeat the formula for stripping the date out of C2 but use A1 as the reference and substitute it in for A1 in the last formula to give:
=DATE(MID(A1,FIND(" ",A1,FIND(" ",A1)+1)+1,4),MONTH(DATEVALUE(MID(A1,2,3)&"-"&1)),TRIM(MID(A1,FIND(" ",A1)+1,2)))-DATE(MID(C2,FIND(" ",C2,FIND(" ",C2)+1)+1,4),MONTH(DATEVALUE(MID(C2,2,3)&"-"&1)),TRIM(MID(C2,FIND(" ",C2)+1,2)))
FORMULAS USED
Text/String Functions
MID
FIND
TRIM
Date/Time Functions
DATE
DATEVALUE
MONTH

How to substract dates with excel - current date with date in format yyyyddmmhhmmss

Is there any option to compare dates with Date format yyyymmddhhmmss with the current date?
Basically one of my external source have this type of date and I have to compare this date with the current date and check difference in between. I have tried to split those date with LEFT,MID,RIGHT functions, so basically, I have two columns - first with date, second with time, but I cannot find any option to subtract current date with date in column, because results are not coming correct.
Sample of date: 20161112203545
after splitting: 2016-11-12 20:05:45.
Any ideas?
Image produced below with formulas is self explanatory.
Your date :20161112203545 in D4
Formula to convert date in E4 :
=DATE(LEFT(D4,4),MID(D4,5,2),MID(D4,7,2))+TIME(MID(D4,9,2),MID(D4,11,2),RIGHT(D4,2))
Today's Date in F4 : =TODAY()
Formula to get date difference in days in G4 : =DATEDIF(F4,E4,"d")
EDIT
The alternative to Excel DATEDIF would be a User defined function (UDF) that internally uses the VBA DATEDIFF function:
This UDF accepts three parameters:
Start_Date: The days from which the period begins.
End_Date: It is the last date of the period that you wish to calculate.
Unit: It specifies the interval by which you want the difference. Here the unit accepts following values.
Value Description
YYYY Year
Q Quarter
M Month
Y Day of year
D Day
Public Function xlDATEDIF(Start_Date As Date, End_Date As Date, Unit As String) As String
xlDATEDIF = DateDiff(Unit, Start_Date, End_Date)
End Function
In this case usage will be, put formula in H4 =xlDATEDIF(F4,E4,"D")
HTH
Taking the date as
20161112203545 after splitting: 2016-11-12 20:05:45
Is going to cause you some issues as Excel assigns date values with a serial number, and it's going to throw that number off. You could use the =today() function and set it up where you have the date entered, say it is in cell A1, then =A1-today() (formatted as a number) should give you the difference in the amount of days.
Microsoft explanation of using Dates in Excel

Excel: Convert calendar week (format ww.yyyy) into date

I need to convert an Excel calendar week date into the actual date.
Excel calendar week format = ww.yyyy (e.g. 31.2014);
Expected output = 7/28/2014 (return the Monday of the week)
What formula should I use ?
It's a bit of a pain: there's no direct function. If A1 contains the year, and A2 contains the week number then,
=MAX(DATE(A1,1,1),DATE(A1,1,1)-WEEKDAY(DATE(A1,1,1),2)+(A2-1)*7+1)
will return the date corresponding to the Monday of that week in that year.
To test it, use =WEEKNUM() and =YEAR() on the computed result, along with =TEXT(,"DDD") to prove it's a Monday.
If #Bathsheba's response works for you, you can do everything in cell B1 with the following command
=MAX(DATE(LEFT(B1,FIND(".",B1)),1,1),DATE(LEFT(B1,FIND(".",B1)),1,1)-WEEKDAY(DATE(LEFT(B1,FIND(".",B1)),1,1),2)+(MID(B1,FIND(".",B1)+1,2)-1)*7+1)
This allows you to put YYYY.WW in B1 and it splits it up for you in the calculation.

How do you parse a substring from an Excel cell?

I have a column with the following values:
month
201201 // means January of 2012
201102 // means February of 2011
201203
201304
...
201307
201106
I need to create a new column that would be convert the last two numbers in the month column into the name of the month, so that 201201 will become Jan-2012, 201106 should become Jun-2011.
How can I parse the last two characters?
This gives you the formatting you want:
=TEXT(DATE(LEFT(A1,4),RIGHT(A1,2),1),"MMM-YYYY")
=DATE(MID(A2,1,4),MID(A2,5,2),1)
You can use LEFT and RIGHT functions and concatenate the results. Assuming the first value is in A2:
=(1&"-"&RIGHT(A2,2)&"-"&LEFT(A2,4))*1
And format the cell as mmm-yyyy.
This assumes that your regional settings have dates as dd/mm/yyyy format.
Otherwise, you'll have to switch the month and date around.
=(RIGHT(A2,2)&"-"&1&"-"&LEFT(A2,4))*1

Convert an Excel formula to an Access query

Is there a way to convert the below Excel formula to a query/criterion in Access?
L2 = Date
J2 = Another Date
Z1 = Todays Date
I think it is calculating the number of days in between two dates but not sure how to do this in an Access query.
IF((AND((L2<1),(J2>1))),(NETWORKDAYS(J2,$Z$1)-1),0)
As you may by now have gathered, the question does not really make sense. NETWORKDAYS does indeed calculate the number of “whole working days excluding weekends and any dates identified in holidays” between two dates (here whatever date is in J2 and whatever is ‘Today’/Z1) and IF makes that calculation contingent upon the result of the AND function being TRUE. The AND function results in TRUE provided both the ‘date’ in L2 is before 1/1/1900 and the date in J2 is after 1/1/1900 (because to Excel 1/1/1900 is Day 1 where dates are concerned [unless one opts for the 1904 date system]).
But Excel does not recognise a date before Day 1, whether a negative number or a decimal number. For example Day 1.5 is noon on 1/1/1900 whereas in the same format Day 0.5 shows as 0/1/1900.
So in essence, L2 is not a date of any real use to the formula, hence I believe why no answer yet to your question. But this is too long to fit in a comment hence my answer: “No.”!
Set a reference to Excel.
Option Compare Database
Private Sub Command2_Click()
MsgBox GetNetWorkDays(#1/29/2017#, #2/8/2017#)
End Sub
Function GetNetWorkDays(startDate As Date, endDate As Date) As Integer
GetNetWorkDays = WorksheetFunction.NETWORKDAYS(startDate, endDate)
End Function
Put the button on a Form and click the button!
If you want to calculate number of days between days than you can use below built in function "DateDiff" which will solve it out. Datediff("D",,)
Ex. Datediff("D",L2,Z1)

Resources