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"))
Related
So my problem is I have a Date that is defined as General in Excel as this "10 JUL 2021 10:30" I want to make it Excel Date.
kindly have a look at my picture for detailed understanding.
Need any kind of solution to automate this VBA or any formula,
Thanks in advance
If you need VBA to extract Date, please use the next function:
Function TextToDateX(txt As String) As Date
Dim arrD, arrM
arrM = Split("JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC", ",")
arrD = Split(txt, " ")
TextToDateX = DateSerial(CLng(arrD(2)), Application.match(arrD(1), arrM, 0), CLng(arrD(0))) + CVDate(arrD(3))
End Function
It can be tested as:
Sub testTextToDateX()
Dim txt As String, d As Date
txt = "10 JUL 2021 10:30"
d = TextToDateX(txt)
Debug.Print Format(d, "dd/mm/yyyy hh:mm")
End Sub
DateValue is localization related. Microsoft states: "DateValue recognizes the order for month, day, and year according to the Short Date format that you specified for your system". So, it may work or not.
In my case it doesn't...
I just tried using the DateValue() worksheet function, and every worked fine (normal Excel formula, no VBA needed):
=DATEVALUE("07 JUL 2021 10:30")
One thing that might be interesting for you: there exists a cell formatting (dd mmm yyyy hh:mm), which you seem to be using. (When you apply this on any cell, the value inside gets automatically formatted into that kind of date format.)
Edit, based on comment:
In case there's a comma in your date string, remove it first and then apply the DateValue() function:
=DATEVALUE(SUBSTITUTE("07 JUL 2021, 10:30",",",""))
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 an Excel file that must work for english & french computer.
I need to concatenate dates in the format yyyy-mm-dd, so for my english user, I need to do:
="your date = "&TEXT(A1,"yyyy-mm-dd")
That works, but not in french Excel, where I need to do
="your date = "&TEXT(A1,"aaaa-mm-jj")
How can I print the same date to both my users, independently of theirs Excel langage locale?
English TEXT function vs French TEXTE function.
And yes, the date format string are localized...
To get the date in right format use:
=YEAR(A1) & "-" & RIGHT("0" & MONTH(A1),2) & "-" & RIGHT("0" & DAY(A1),2)
I found this link which seems to treat your issue :
If you are using the TEXT worksheet function because it is part of a larger formula, then you can instruct the function itself to use a different language for its output. You do this by including a language code (formally called an LCID) within brackets, in this manner:
=TEXT(A1,"[$-409]mmmm, yyyy")
Where you could specify the text to display according to the language you want.
I used to cope with this issue as follow assuming you're on a eng MSExcel:
1. I create a named formula (returns true when it's a fr region MSExcel)
name: xlFR
refers to: = text(Today(),"d") = "d"
click OK
2. in your formula you decide on xlFR
ex: = Text(today(), if(xlFR,"jj-mm-aaaa","dd-mm-yyyy") )
That's it.
Hope this helps.
I use this vba function in my formulas:
Function DateToText(MyDate As Date)
DateToText = Format(MyDate, "DD/MM/YYYY")
End Function
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.
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"