Date difficulty

Robert E Lee

Active Member
Joined
Aug 10, 2005
Messages
266
Hi all

I have extracted the date from a file name using the following code

Code:
Date1 = Right(Left(Name, Len(Name) - 4), 9)

I now need to use this text date to create a date in DD/MM/YYYY format so using Date1 i tried this

Code:
Date2 = Left(Date1, 3) & "/" & Right(Date1, 6)
which failed

doing the same thing in two stages and then concatenating the results did produce the desired result

Code:
Month = Left(Date1, 3)
Year = Right(Date1, 6)
Date2 = Month & "/" & Year

I would love to know why the second is acceptable but not the first

Also, if I may add a second question, when the "Date2" is entered the date shows as 01/09/2109, is there any way I can make it 30/09/2019 and to have the last day of succeding months

Many thanks

Rob
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
you have not shown what your date string looks like but if it is a string date, then try using the DateValue Function

Code:
Date1 = DateValue(Right(Left(Name, Len(Name) - 4), 9))

This should change the string in to a Date & the function recognizes the order for month, day, and year according to the Short Date format that you specified for your system


Dave
 
Last edited:
Upvote 0
Thank you both for your comments. I am creating a .csv for import to Sage and this requires that there be a text date in one field and a date format date in another. The Date1 works perfectly even though from what has been said it shouldn't and this is used for the text date.

The second part of my question was asking why it should be that the first set of code for the date format date doesn't work and the second does.

Finally when the non-text date is entered it reads as 01/09/2019; I would like to be able to change this to 30/09/2019 or to whatever day is the last of the month in question

Thanks
Rob
 
Upvote 0
Code:
Month = Left(Date1, 3)
Year = Right(Date1, 6)
Date2 = Month & "/" & Year
I presume that is not your actual code because you can't use Month and Year as names of variables.

You did not post what Name looks like (as requested by Special-K99 and dmt32).
I'm guessing it looks like this : 0mm00yyyy####

If so, the reason why your first code doesn't work is because it produces a string like : 0mm/00yyyy
With the second code, presumably the variables are defined as numbers so produce : mm/yyyy

Try this instead (also produces the last day of the month) :
Code:
Dim name$, date2 As Date
name = "011002019####"
date2 = WorksheetFunction.EoMonth(DateSerial(Mid(name, 6, 4), Left(name, 3), 1), 0)
 
Last edited:
Upvote 0
I presume that is not your actual code because you can't use Month and Year as names of variables.
Forget that - I think you can.
 
Upvote 0
Hi

I apologise for not posting the filename which is Dev CB Sep 2019 and also confess that dmt32 was correct in that my "9" produces an incorrect result having a spurious . at the end.

So I got that bit sorted and would like to ask footoo how to use the worksheet function using the result produced by my first line of code.

Thanks

Rob
 
Upvote 0
Code:
Dim name$, date2 As Date
name = "Dev CB Sep 2019.xlsx"
date2 = WorksheetFunction.EoMonth(DateValue("1/" & Mid(name, 8, 3) & "/" & Mid(name, 12, 4)), 0)
 
Upvote 0
Hi, you could also try setting the date2 variable using this line:

Code:
date2 = DateAdd("m", 1, Mid(name, 8, 8)) - 1
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,237
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top