I`ve vba code to display date in a specific format like below
If Not IsNull(Data.getAttribute("recorded"))) Then
Range("BG3") = Format((Data.getAttribute("recorded")), "dd / mmm / yyyy")
End If
This code actually works in my local. But in server it showed format as '02.14.2013'.
Due to wrong date format I went in some problem.
What might be the cause for this?
Someone please help me.
Try and add the following before the Range statement:
Range("BG3").NumberFormat = "#" 'set to display as text
My suggestion is to try:
If Not IsNull(Data.getAttribute("recorded"))) Then
Range("BG3").Value2 = Format((Data.getAttribute("recorded")), "dd / mmm / yyyy")
End If
And if that is still a problem
If Not IsNull(Data.getAttribute("recorded"))) Then
Range("BG3").Value = Data.getAttribute("recorded")
Range("BG3").NumberFormat = "dd / mmm / yyyy"
End If
But I still stand by putting a break point somewhere strategic and stepping through the code to make certain the the number appears in the cell in the incorrect format as opposed to being reset later on.
Related
I am using this below code
Range("A1") = Format$(Now, "dd/mm/yyyy h:mm:ss AM/PM")
and the o/p as shown in A1 is 02/12/2020 20:08:38 which is incorrect as today is 12th Feb 2020, so it should be 12/02/2020 20:08:38. Just the dd is interchanged with mm and there is no AM/PM. Already have checked the control panel--region and it's English (United Kingdom). OS is Win10. My system shows 12/02/2020 as the date. When I write 12/02/2020 manually it accepts as it is and doesn't change it.
Don't have any clue about it. Thanks for any help.
Instead of using Format$ (which returns a String anyway, not an actual datetime), change the NumberFormat of the cell:
Range("A1").NumberFormat = "dd/mm/yyyy h:mm:ss AM/PM"
Range("A1").Value = Now
I have dates stored in strings, sometimes in the form of "Nov-2018" and sometimes in the form of "11/1/2018". I want to change them universally to "Nov 2018". Just month and date.
I wrote the following function.
Function FnDateExtract(fnFile, fnTab, fnRow, fnColumn)
Dim RawExtract As String
With Workbooks(fnFile).Worksheets(fnTab)
RawExtract = .Cells(fnRow, fnColumn).Text
FnDateExtract = Format(RawExtract, "mmm yyyy")
End With
End Function
I keep getting into this problem where the function would read strings '11/1/2018' and 'Nov-2018' and return the result 'Nov 2019'. I have no clue why 2019 (but the problem only appeared from January 1, 2019 onward. How strange.)
Question:
As a temporary solution, is there a way to return the year minus 1? Something akin to FnDateExtract = Format(RawExtract, "mmm yyyy - 1")?
If you could help me solve the problem why the function always misreads strings by exactly 1 year, that'd also help a lot. Thanks!
Edit
RonRosenfeld below suggested that the function would only extract the current year. I did a few tests and it seems like it would also extract 2017 as 2019. Not sure why that is. Still looking for a solution.
Use the IsDate check combined with the CDate conversion to achieve the correct string representing a date.
Option Explicit
Function FnDateExtract(fnFile As String, fnTab As String, fnRow As Long, fnColumn As Variant)
With Workbooks(fnFile).Worksheets(fnTab)
If IsDate(.Cells(fnRow, fnColumn).Text) Then
FnDateExtract = Format(CDate(.Cells(fnRow, fnColumn).Text), "mmm yyyy")
End If
End With
End Function
I am creating a report page for my excel template and I want to add a "date" section below.
I simply wrote the code as this;
Worksheets("Report_TEMP").Range("H46:J46") = Format(Date, "dd-mm-yyyy")
I don't know why, but the report gives me the date as "mm-dd-yyyy".
It show today as 10.12.2017 like US system.
How can I make it sure to write it as 12.10.2017 ?
You should apply the formatting to the cells:
With Worksheets("Report_TEMP").Range("H46:J46")
.NumberFormat = "dd-mm-yyyy"
.Value = Date
End With
You can use the format as DD.MM.YYYY as mentioned below
Worksheets("Report_TEMP").Range("H46:J46") = Format(Date, "dd.mm.yyyy")
It will give us the desired output. Thanks
you can use as a string, like this :
Worksheets("Report_TEMP").Range("H46:J46") = Cstr(Format(Date, "dd-mm-yyyy"))
I have VBA code which pull the details from .msg file (outlook files) and update in excel sheet. While reflecting the date column, it is showing as "3/9/2016 11:03:27 AM" but I want to show only date and not time.
I used the format option i.e.
Sheet2.Cells(Row + 1, 23) = VBA.Format(sentDate, "dd/MM/yyyy")
but it is showing the date a "8/3/2016 00:00". I want to reflecting only date and nothing else. Please guide me as to what all changes required to reflect only date.
Give this a try:
Sheet2.Cells(Row + 1, 23).Value = sentDate
Sheet2.Cells(Row + 1, 23).NumberFormat = "mm/dd/yyyy;#"
Yet, I have a feeling that the cell does not really contain a date but merely text. So, please also try to change the .NumberFormat to the following first:
Sheet2.Cells(Row + 1, 23).NumberFormat = "General"
If the dates are now showing all as numbers then these are actual dates and the first proposal should work. If not, then these are not dates yet and you'll have to convert the text (which looks like dates) to dates first.
For more information you might want to read this: Difference between date and time w/out work week Excel
I grab dates from one spreadsheet and output them onto another spreadsheet. After grabbing the date, when I debug.print it is in the correct format. When I output the date, debug.print also displays the correct format. However, the format on the spreadsheet the value has just been sent to, doesnt show the correct format.
I am using:
Sheets(SheetName).Cells(RowNum, ColNum).Value = Data
Sheets(SheetName).Cells(RowNum, ColNum).NumberFormat = "dd/mm/yyyy"
after I have pasted the value, but the months and days are still switched the wrong way.
Is there something I am doing wrong?? If I right click the cell it thinks it's date is dd/mm/yyyy but instead of 4th Sept it is showing 9th April.
This might be some trouble with localization:
Try using NumberFormatLocal, if DanielCooks tip didn't help ;)
edit: erlier it was statet by mister Cook, to check if the given data is correct.
edit:
With my german version I have quite some trouble to use / as the seperator, that is why i tryied with this code .NumberFormat ="dd-mm-yyyy;#" - works fine; I can switch days and month as I like.
edit:
With .NumberFormatLocal = "TT/MM/JJJJ" I have to use the german shorts for day, month and year, but now I can use / as the seperator.
You should experiment a litte bit with some formats strings ;)
Sorry to resurrect an old post, however I had a problem with VBA outputting a valid date as US style with the actual date changed for example 1st May changed to 5th Jan. I came upon this post but I didn't find the answer I needed here and now that I have worked it out I thought I would share it:
The key is not to define the variable storing the date as a "date" but as a "double", e.g.
Dim ReportDate As Double
ReportDate = Date
Range("E6").Value = ReportDate
This works as it outputs the numeric "date value" which excel then formats locally e.g. 41644 gets formatted as "05/01/14" using UK format or "01/05/14" using US format.
Hope this proves useful to other people (probably me when I forget how I solved it in six months time).
In the end I had to format the cell as "TEXT" to keep the correct format
(1) You need to define the variable to "Date" type to read the input, then set the date format before assigning it to the date variable.
(2) You also need to format the date output to make it work !
'**Read the date input****
Dim date1 as Date
Range("B2").NumberFormatLocal = "dd/mm/yyyy"
date1 = Range("B2").Value
'**Output the date****
Range("E2").Value = date1
Range("E2").NumberFormatLocal = "dd/mm/yyyy"