I need to convert a string variable to a date variable in a VBA macro, to process the date to get the mMonth and year in order to name the worksheet
Call GetMonthandYear(MonthYear, FileDate)
ActiveSheet.Name = MonthYear
I looking to create a method called GetMonthandYear. FileDate is a String in a date format, dd.MM.yyyy HH-mm-ss
. If I can change the variable to a date, I can change the format to MMMM/yyyy
and then use ToString
, I think, and assign it to MonthYear
.
Is there a way to change the string to a date?
You can get Date using CDate function but it requires
-
to be replaced by:
and.
to be replaced by/
.Have you tried using the CDate and Format functions?
There are several problems with your proposed aaproach:
dd.MM
orMM.dd
. Since you know the format in advance, extract the month and year directly.\
is not a valid character for sheet names. I've used_
, substitute as you wishCall it like this