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.
Related
I am currently using Excel 2010 and I have a column that contains date in such format (dd.mm.yyyy). I would like to change the dots to slash (/). I have tried using rng.Cells(1, 6) = WorksheetFunction.Substitute(rng.Cells(1, 6), ". ", "/")
However, when I do that, the result I get is different from my original date. For example, 02.11.2011 will become 11/02/2011.
The column that contains (dd.mm.yyyy) is in a date format
May I know why is that or is there another way of doing?
Is the column formatted as a date? If so, you can try
rng.Cells(1, 6) = Format(CDate(rng.Cells(1, 6)), "dd/MM/yyyy")
If it's an actual date value, then use NumberFormat
Range("B1").NumberFormat = "d/m/yyyy"
Range("B2").NumberFormat = "m/d/yyyy"
Range("B3").NumberFormat = "m-d-yyyy"
Im using this
splR = Split(oRange.Value, ".")
If UBound(splR) = 2 Then oRange.Value = Format(DateSerial(splR(2), splR(1), splR(0)), RegionalDateFormat) Else oRange.Value = oRange.Value
Function RegionalDateFormat(Optional outS As String)
Dim DateOrder As String
Dim DateSeparator As String
With Application
DateSeparator = .International(xlDateSeparator)
Select Case .International(xlDateOrder)
Case Is = 0
DateOrder = "mm" & DateSeparator & "dd" & DateSeparator & "yyyy"
Case Is = 1
DateOrder = "dd" & DateSeparator & "mm" & DateSeparator & "yyyy"
Case Is = 2
DateOrder = "yyyy" & DateSeparator & "mm" & DateSeparator & "dd"
Case Else
DateOrder = "Error"
End Select
End With
If outS = "Sep" Then outputS = DateSeparator Else outputS = DateOrder
RegionalDateFormat = outputS
End Function
Let's clarify that an Excel spreadsheet does not have a "date" data type per se. Excel's approach to dates is to store an integer, which you can choose to format as a date. So, for example, 43467 is the integer representing Jan 2 2019. (And the decimal part represents time during that day, so 43467.333.. represents Jan 2 2019 8:00 am.)
Try entering a date, then right-click > "Format cells" to format that cell as a number, to observe what I'm talking about.
In the same way, you can cause Excel to render that cell in the date format you want using the same method: select the cell > right-click > "Format cells", choose Custom, and enter a custom format, like dd.mm.yyyy.
Once you see that works, then your remaining task is simply to perform that same action from VBA, which would be with a statement like:
Range("B1").NumberFormat = "dd.mm.yyyy"
... as pointed out by others.
Now, if your initial attempt to get Excel to format your date cell as a number fails (it maintains its date appearance) then the date you see in the cell is actually stored as a string, and won't function as a date (for example to do date arithmetic). So you'll need to decide how to proceed -- perhaps convert these strings to "dates" (ie: numbers formatted as dates).
I want to put today's date (4 November 2018) into a cell in this (dd/mm/yyyy) format:
04/11/2018
However every attempt so far leads to (mm/dd/yyyy format):
11/04/2018
Original code is:
Dim sToday As String
sToday = Date
Cells(nCurrentRow, nCurrentColumn) = sToday
What I have tried includes:
Cells(nCurrentRow, nCurrentColumn).Value = sToday
and:
Cells(nCurrentRow, nCurrentColumn).Select
Selection.NumberFormat = "dd/mm/yyyy;#"
and:
sToday = Format(Date, "dd-mm-yyyy")
When I output the string in a message box, it always appears in the desired format: 04/11/2018
Even when I post the date as a plain string in the correct format 04/11/2018 the format still changes!
Windows 10 regional settings are as I require (UK date format).
The cell format is also set to dd/mm/yyyy.
I think your algorithm is getting bollixed by VBA US-Centric date preferences.
No matter your windows short date regional setting, all you need do is insert the variable as a Date (not as a string).
When VBA converts today's date to a string, it is in the US Format. Therefore numberformat in the destination cell will not have any affect on a string, nor will the VBA format function. Both of those act on numbers/dates but not on strings, the way you are using it.
For example, try:
With Cells(1, 1)
.NumberFormat = "dd/mm/yyyy"
.Value = Date
End With
or, using your variable assignments:
Dim dToday As Date
dToday = Date
With Cells(nCurrentRow, nCurrentColumn)
.Value = dToday
.NumberFormat = "dd/mm/yyyy"
End With
Sub test()
Dim sToday As String
sToday = Date
sToday = Format(sToday, "dd mm yyyy")
MsgBox sToday
Cells(1, 1).Value = sToday
End Sub
I tried this, and I got the wished result.
The workaround is to save the date in a string and format the string.
How can I get today's date in a cell in dd/mm/yyyy format?
Since your configuration is UK format:
Ctrl+;
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 a column of dates that converted from MS Project into excel as strings.
The format that converted is similar to the following: "March 31, 2014 8:00AM"
I want to convert "March 31, 2014 8:00AM" into type Date but, since it isn't formated MM-DD-YYYY it is not letting me.
Any suggestions?
If the value is in cell A2, for example, you could do the following:
=DATE(YEAR(A2),MONTH(A2),DAY(A2))
For a pure VBA solution, expanding on #cirrusone's suggestion, try this:
Public Sub lime()
Dim dt As Date
' Convert the date string into a Date value.
' Assumes your date string is in cell A1 in the active worksheet of the active
' workbook.
dt = CDate(Cells(1, 1))
' This will print the date with ".." as separators to show that it worked, i.e.
' it should be able to understand every part of the date string passed to it
' after having converted it to a Date value.
MsgBox DatePart("d", dt) & ".." & DatePart("m", dt) & ".." & DatePart("yyyy", dt) & ".." & _
DatePart("h", dt) & ".." & DatePart("n", dt) & ".." & DatePart("s", dt)
End Sub
I put some comments to explain what it's doing. DatePart is a useful function to extract bits of the date (and useful to know in general).
I am using DateTime.Now in my Excel Macro to show the current timestamp.
It shows timestamp in "dd-MM-yyyy hh:mm:ss" format.
Instead, how can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?
Try with: format(now(), "yyyy-MM-dd hh:mm:ss")
DateTime.Now returns a value of data type Date. Date variables display dates according to the short date format and time format set on your computer.
They may be formatted as a string for display in any valid date format by the Format function as mentioned in aother answers
Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Format(Now(), "yyyy-MM-dd hh:mm:ss")
If some users of the code have different language settings format might not work. Thus I use the following code that gives the time stamp in format "yyymmdd hhMMss" regardless of language.
Function TimeStamp()
Dim iNow
Dim d(1 To 6)
Dim i As Integer
iNow = Now
d(1) = Year(iNow)
d(2) = Month(iNow)
d(3) = Day(iNow)
d(4) = Hour(iNow)
d(5) = Minute(iNow)
d(6) = Second(iNow)
For i = 1 To 6
If d(i) < 10 Then TimeStamp = TimeStamp & "0"
TimeStamp = TimeStamp & d(i)
If i = 3 Then TimeStamp = TimeStamp & " "
Next i
End Function
this worked best for me:
Cells(partcount + 5, "N").Value = Date + Time
Cells(partcount + 5, "N").NumberFormat = "mm/dd/yy hh:mm:ss AM/PM"
Copy and paste this format yyyy-mm-dd hh:MM:ss in format cells by clicking customs category under Type
Timestamp in saving workbook path, the ":" needs to be changed. I used ":" -> "." which implies that I need to add the extension back "xlsx".
wb(x).SaveAs ThisWorkbook.Path & "\" & unique(x) & " - " & Format(Now(), "mm-dd-yy, hh.mm.ss") & ".xlsx"
It can work as easy as this, choose the location you want, in this case I choose D3
Sheets("Put your Sheet's name here").Range("D3") = Now
Example, my sheet is called Sources
Sheets("Sources").Range("D3") = Now
Use the Format function.
Format(Date, "yyyy-mm-dd hh:MM:ss")