Recently I've been trying to write a macro code for converting a range of dates in dd.mm.yyyy to dd-mmm-yyyy format.
So, I first replaced the "." with "-" and used numberformat = dd-mmm-yyyy
There seems to be an error in the output as it is giving month as date and date as month, e.g., 07.01.2020 is showing as 01-Jul-2020.
Following is the code:
sub dateformat()
range("A2").replace what:="." replacement:="-"
range("A2").numberformat = "dd-mmm-yyyy"
end sub
Kindly help if there's a solution to this.
Give this a try:
Sub fixdate()
Dim s As String
With Range("A2")
s = .Text
.Clear
.NumberFormat = "dd-mm-yyyy"
.Value = DateSerial(Right(s, 4), Mid(s, 4, 2), Left(s, 2))
End With
End Sub
Related
I have an exported file that gives me a list of long dates in format General.
ex: Friday, August 28, 2020
I am trying to convert them into short dates. I've tried using CDate function, but I get a mismatch error. I find this odd because the cell has the exact long date form.
I've tried running a ton of code. Here's the most recent one I tried. It changs the cell format into Long Date. then uses Cdate and gets a mismatch error.
Sub formatdate()
'
' Macro1 Macro
'
Range("J2").Select
Selection.NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
MsgBox (CDate(Range("J2")))
End Sub
Any help would be much appreciated!
As #scottcraner says:
Dim v
With Range("J2")
v = .Value
v = Mid(v, InStr(v, ",") + 1, 100)
.Value = DateValue(v)
End With
So I have cells containing strings of date, such as:
14/04/2019 10:13:18 AM
how can I convert it to DateTime using vba?
I've tried using .NumberFormat but some of the cells got converted and some didn't:
My code is
Sub ConvertToDateTime()
With Range("Data[Modified On]")
.NumberFormat = "dd/mm/yyyy hh:mm:ss AM/PM"
.Value = .Value
End With
End Sub
And how do I insert converted value to a new column?
I've created a new column with:
Dim Table As ListObject
Dim newColNum As Integer
Set Table = ActiveSheet.ListObjects("Data")
Table.ListColumns.Add.Name = "New Header"
Can I do it without looping?
Some of the cell do got converted but some don't
That's because the date format on your PC is different than the data (it is mm/dd/yyyy while the data is in "dd/mm/yyyy")
This can't be fixed without looping. (as far as I know)
To fix that, you might need to do something like this:
Sub ConvertToDateTime()
Dim Cell As Range, h As Long, c As Long
Range("Data[New Header]").NumberFormat = "dd/mm/yyyy hh:mm:ss AM/PM"
c = Range("Data[New Header]").Column
For Each Cell In Range("Data[Modified On]")
If Right(Cell.Value, 2) = "PM" Then h = 12 Else h = 0
Cells(Cell.Row, c).Value = DateSerial(Mid(Cell.Value, 7, 4), Mid(Cell.Value, 4, 2), Left(Cell.Value, 2)) + _
TimeSerial(Mid(Cell.Value, 12, 2) + h, Mid(Cell.Value, 15, 2), Mid(Cell.Value, 18, 2))
Next
End Sub
I'm trying with VBA to convert a date format in a table column so that I can use it in formulas.
The column contains dates with format "general", not "date" and looks like this: 13/04/2019. So it needs to be dd/mm/yyyy and recognized as a date.
I've looked up online and struggle to find something that works.
I've tried:
.numberformat = "dd/mm/yyyy"
but it didn't work
This is the code I'm trying.
With wsDormant.ListObjects("Table_Dormant_Stock").ListColumns.Add
.DataBodyRange.Formula = "=DATE(RIGHT([#[Days Last Sold]],4), MID([#[Days Last Sold]],3,2), LEFT([#[Days Last Sold]],2))"
End With
'Change date to amount of days
With wsDormant.ListObjects("Table_Dormant_Stock").ListColumns.Add
.DataBodyRange.Formula = "=DAYS($C$8,[Days Last Sold])"
wsDormant.ListObjects("Table_Dormant_Stock").ListColumns("Days Last Sold").DataBodyRange.Value = .DataBodyRange.Value
.Delete
End With
I'm getting #value when I try the formula.
It needs to convert the cell format to date. The formulas that uses dates don't seem to like the date format in dd/mm/yyyy. But are happy with yyyy/mm/dd.
You should first convert your "pseudo-dates" into "real" dates. Select the cells and run:
Sub FixDate()
Dim d As Date, r As Range, v As String
For Each r In Selection
v = r.Text
r.Clear
arr = Split(v, "/")
d = DateSerial(arr(2), arr(1), arr(0))
r.Value = d
r.NumberFormat = "dd/mm/yyyy"
Next r
End Sub
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+;
Platform: OS window 7
Excel: 2007
Issue : I had a .csv file with some data and date capture from other place. The data(date) is located in cell A2 having the category under Date. (e.g 18/3/2014)
Firstly there is this Locale (location), stated English (Singapore). I wanted to change it to English (United States)
After that then change the date format to "M/D/YYYY"
What i had tried:
ExcelApp.ActiveWorkbook.Sheet1.Range("A2").NumberFormat = "M/D/YYYY"
it prompted me run time error "438"
object doesn't support this property or method.
how can make it that it return me the cell value as "3/18/2014"?
If you look in A2 and see:
18/3/2014
then its either a String or a formatted date. it does not matter which ;run this to fix it:
Sub FixDate()
Dim s As String
With Range("A2")
s = .Text
ary = Split(s, "/")
.Value = DateSerial(ary(2), ary(1), ary(0))
.NumberFormat = "m/d/yyy"
End With
End Sub
EDIT#1
If you want to Dim ary, then:
Option Explicit
Sub FixDate()
Dim s As String, ary
With Range("A2")
s = .Text
ary = Split(s, "/")
.Value = DateSerial(ary(2), ary(1), ary(0))
.NumberFormat = "m/d/yyy"
End With
End Sub
Well, a CSV file is just a Comma Separated Value File, so it doesn't support any of the cell formatting that an XLS or XLSX file would. So what you can do is:
If it only contains a date.
Dim dt As Date : dt = cdate(Range("A2").Value)
Range("A2").Value = Format(dt, "MM/d/yyyy")
If it contains a date and time and you only want the date.
Dim dt As Date : dt = cdate(Range("A2").Value)
Range("A2").Value = FormatDateTime(dt, 2)
if it contains a date and time and you want both.
Dim dt As Date : dt = cdate(Range("A2").Value)
Range("A2").Value = FormatDateTime(dt)