I wrote a Macro to simplify a process at work.
I am trying to figure out how to fix this date so when the macro is run it isn't missing an "."
ex)
here is my code:
Dim currentDate As String
currentDate = Left(Replace(Date, "/", "."), 5) + Right(Date, 2)
To format date as a string use Format function and specify the format in a string. For instance:
Format(Date, "yyyy-mm-dd") 'gives 2020-01-08
I believe the following should add the desired dot:
Dim currentDate As String currentDate = Left(Replace(Date, "/", "."), 5) & "." & Right(Date, 2)
Related
Hi I would like to put into a STRING the following formula
Dim stringAppoggio As String
Dim myMonth As String
myMonth = "January 2020"
stringAppoggio="=DAY(EOMONTH(DATEVALUE(01-'&myMonth&'-&YEAR(myMonth)),0))"
It doesn't really give me a syntax error, but I don't see the result of the formula in the String
Thank you
Well, as per my comment, there are a few mistakes here:
You have used single quotes instead of double quotes to seperate your variable from VBA formula syntax
You have forgotten the quotes at all around your second myMonth variable
You have created a formula that simply won't work
Keep in mind, your variable is not just a month but a string holding a month and year > "January 2020", therefor DATEVALUE won't need the 01- and YEAR(myMonth) to work. Let me explain:
=DATEVALUE("January 2020")
Will return Integer 43831, or in other words: 1-1-2020. Then secondly, EOMONTH will return the end of that same month as an Integer, whereas DAY will return the number of that day. So your formula would read:
=DAY(EOMONTH(DATEVALUE("January 2020"),0))
Now to write this in VBA:
Dim stringAppoggio As String
Dim myMonth As String
myMonth = "January 2020"
stringAppoggio = "=DAY(EOMONTH(DATEVALUE(""" & myMonth & """),0))"
You can check that it works:
Debug.Print Evaluate("=DAY(EOMONTH(DATEVALUE(""" & myMonth & """),0))")
Note: See the triple quotes? That's because we need to feed DATEVALUE a string within quotes to work, otherwise it wouldn't be a string and will return an error
Dim stringAppoggio As String
Dim myMonth As String
myMonth = Chr(34) & "January 2020" & Chr(34)
stringAppoggio = "=DAY(EDATE(" & myMonth & ",1)-DAY(" & myMonth & "))"
I can format a date in a specific date format, but the formatted string seems not possible to format anymore.
It wouldn't be a problem, because I could save the old format. But the thing is that I also have to read a string in the specific format from an excel cell and format it.
I use the german date system. (Mi = Wednesday)
UI_Main.txt_BeginnDatum.Value = Format(UI_Date_Picker.Date_Picker.Value, "ddd dd mmm yyyy")
That's how I get the date from the datepicker. It's now formatted in the specific date format I'm talking about (Mi. 09 Jan 2019).
The default format from the picker is dd.mm.yyyy.
strBeginDate_g = txt_BeginnDatum.Value
strTemp = strTemp & "Nr. " & Format(strBeginDate_g, "yyyy-mm-dd")
Here I try to write the date in another format, but the output is just the same as before.
Of course I could write my own function but I am sure format is supposed to handle this.
You could write a custom format, like so
Function FormatYYYYMMDD(strDateIn As String) As Date
Dim a() As String
Dim s As String
a = Split(strDateIn, Chr(32))
a(0) = vbNullString
FormatYYYYMMDD = CDate(Format(a(1) & "/" & a(2) & "/" & a(3), "yyyy-mm-dd"))
Erase a
End Function
The Format function can only format a numeric value. But you give it a string strBeginDate_g as parameter.
Instead give it the value as parameter:
strTemp = strTemp & "Nr. " & Format$(UI_Date_Picker.Date_Picker.Value, "yyyy-mm-dd")
Once you formatted a date with Format() it becomes a string, and you cannot calculate anymore with a string nor can you format it again.
I am trying to get the Date as a string formatted yyyy-mm-dd.
I have tried various things with strange results:
Dim mydate As String
mydate = Date
mydate = Year(Date)
mydate = Month(Date)
mydate = Day(Date)
The first one gives 11/02/ without the year.
I can try to concatenate the data but:
The second gives the year OK
However the third gives month as 2 instead of 02
Similar for fourth.
Any clarification or an example would be very welcome.
Use the Format function from the VBA.Strings built-in module:
Debug.Print Format(Now, "YYYY-MM-DD")
Dim sToday As String
sToday = CStr(Date)
That gives sToday value, e.g. "2020-12-31", in the format of my system's date.
In some VBA, the Format() function is not available. In this case, you can do it the old fashion:
y = cstr(year(now))
m = right("0" + cstr(month(now)),2)
d = right("0" + cstr(day(now)),2)
mydate = y + "-"+ m + "-" + d
I would like to be able to use VBA to display any number between 1-24 as a 2 digit number. Obviously the only ones that have a problem with this are 1-9 which I would like displayed as 01, 02, 03, etc. Is there a way to perform this?
You cannot format an integer variable, you need to use a string variable for formatting.
You can convert the day part of a date to a format with leading zeros using the Day function to extract the day number from the date, and then using the Format function with a "00" format to add a leading zero where necessary
Format(Day(myDate), "00")
myDate is a Date variable containing the full Date value
The following macro can be used as a working sample
Sub Macro1()
Dim myDate As Date
myDate = "2015-5-1"
Dim dayPart As String
dayPart = Format(Day(myDate), "00")
MsgBox dayPart
End Sub
Sure you can format an integer, you just convert it to string within the format command:
formattedIntAsString = Format(Cstr(intValue), "00")
I did it like this:
number_item = 2
number_item = WorksheetFunction.Text(number_item, "00")
This will do the job.
I know it's old, but, to answer the question as clarified, I would use in the built in date formatting functionality.
To modify DeanOC's answer:
Sub Macro1()
Dim dateDate As Date
Dim strDate As String
Dim strDay As String
dateDate = "2015-5-1"
strDate = Format(dateDate, "mm/dd/yy") ' = "05/01/15"
strDay = Format(dateDate, "dd") ' = "01"
MsgBox "The two digit day of """ & strDate & """ is """ & strDay & ""."
End Sub
I have the following code:
minDate = CDate(Table.Cell(i, 4).Range.Text)
But I get a "Type mismatch error".
Table.Cell(i, 4) is in a "dd.mm.yy" format.
I wrote a macro to test this and the date format dd.mm.yyyy is invalid, it seems you must use dd/mm/yyyy (using slashes instead of period) or dd-mm-yyyy (using hyphens instead of periods):
Sub Macro1()
Dim minDate As Date
Dim DateStr As String
' with slashes, ok
DateStr = "27/12/2013"
minDate = CDate(DateStr)
' replace periods w/ slashes, ok
DateStr = "27.12.2013"
minDate = CDate(Replace(DateStr, ".", "/"))
' replace periods w/ hyphens, ok
DateStr = "27.12.2013"
minDate = CDate(Replace(DateStr, ".", "-"))
' type mismatch
DateStr = "27.12.2013"
minDate = CDate(DateStr)
End Sub
So, to solve your problem, you just need to replace all periods in your date to either hyphen or slash:
minDate = CDate(Replace(Table.Cell(i, 4).Range.Text, ".", "/"))
Try
minDate = CDate(Table.Cell(i, 4))
The date conversion performance depends on the date format settings in Region and Language.
The following function ensures a successful conversion:
Function TextToDate(vstrText) As Variant
' Description: Convert Text Date to Date
'--
Dim d As Variant
Dim sDate As String
sDate = Replace(vstrText, ".", "-")
If Right(sDate, 1) = "-" Then
sDate = Left(sDate, Len(sDate) - 1)
End If
If IsDate(sDate) Then
d = CDate(sDate)
Else
d = ""
End If
TextToDate = d
End Function