converting range to date format not working

GlennW81

New Member
Joined
Aug 19, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a line of code that is supposed to convert range (that is currently in dd-mmm-yyyy format) to dd-mm-yyyy. However, the format does not seem to change. When I click on the format cells (from the number section on the Home tab) to check the format it indicates that it is in dd/mm/yyyy format, though it clearly is not and the sample is in the dd/mmm/yyyy format.
The conversion code is:

VBA Code:
wsRData.Range(wsRData.Cells(2, 6), wsRData.Cells(lastrow, 6)).NumberFormat = "dd/mm/yyyy"

Below is a screenshot of displaying the date in the cells and the format selected from the format cells screen.
1667971722923.png


I need the code to correctly change the date to dd/mm/yyyy.
Are you able to help

Cheers
Glenn
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
If you have not applied manual alignment to col F your dates are not real dates but text looking like dates.
To convert them to real dates I usually select the column - Click Data - Text to columns - Finish.
If you can code this, you're good ( I'm not VBA savvy)
 
Upvote 0
As arthur said the date column is most likely text. If this is likely to happen again then you can handle it in the code. This should work, given that your date format is pretty standard.

Rich (BB code):
    ' Replace this
    wsRData.Range(wsRData.Cells(2, 6), wsRData.Cells(lastrow, 6)).NumberFormat = "dd/mm/yyyy"
   
    ' With this
    With wsRData.Range(wsRData.Cells(2, 6), wsRData.Cells(lastrow, 6))
        .NumberFormat = "dd/mm/yyyy"
        .Value = .Value
    End With
 
Upvote 0
Just an adaption on what @arthurbr has posted

Click Data - Text to columns - Next - make sure all the delimiter boxes are cleared - Next - Click the Date option - select the DMY option and click Finish.

or

Select the column and try running the code below
VBA Code:
Sub ConvertDate()

    Selection.TextToColumns Selection.Cells(1, 1), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 4), TrailingMinusNumbers:=True
End Sub

Edit: please try the code by @Alex Blakenburg first as with your data format the
VBA Code:
.Value = .Value
should work
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top