VBA to change text to date

j4ymf

Well-known Member
Joined
Apr 28, 2003
Messages
741
Office Version
  1. 365
Platform
  1. Windows
Hello All

I was wondering is there a way to change text to a date

If I have in cell A1- 01 Jun 20 can that be changed to 01/06/2020
A2 - 02 Jun 20 to 02/06/2020 and so on etc And then replace the hole year as a macros change all the dates in column A

Thank you
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Am I missing something but why use VBA when you can format the column to dd/mm/yyyy?
 
Upvote 0
Am I missing something but why use VBA when you can format the column to dd/mm/yyyy?
If the dates are truly entered as Text, changing the format of the cell will have no impact on it, as Formatting only applies to numeric entries (values and dates) and not text ones.
However, it is rather easy to convert them to valid dates using the "DATEVALUE" function, i.e.
=DATEVALUE(A1)
Then, you can apply any date format you want, since it has been converted to valid date.

If you wanted VBA to do this, one way would be to turn on the Macro Recorder, and record yourself inserting a function and applying this formula and formatting, and copy down for all rows.
You can also hard-code it by Copy/Paste Special Values over top of itself, and deleting the original data.
That will give you most of the code you need, but we can help you make it dynamic (i.e. finding the last row for you), if you need help with that part.
Just post the code here, and we can help you fix it up.
 
Upvote 0
Try the following and custom format the date to your preference

T202007a.xlsm
AB
101 June 202001-Jun-20
202 June 20202-Jun-20
303 June 202003-06-2020
4
1c
Cell Formulas
RangeFormula
B1:B3B1=--TEXT(A1,"dd mmm yyyy")
 
Upvote 0
The DateValue certainly works; I just provided the others as extra information.

N.B. The example prepared with Excel 2010 and system regional settings dd-mm-yy
Custom format to your preference d-mmm-yy is unambiguous

T202007a.xlsm
ABCD
101 June 202001-Jun-2001-Jun-2001-Jun-20
202 June 20202-Jun-202-Jun-202-Jun-20
303 June 202003/06/202003/06/202003/06/2020
4
1c
Cell Formulas
RangeFormula
B1:B3B1=--TEXT(A1,"dd mmm yyyy")
C1C1=--A1
D1:D3D1=DATEVALUE(A1)
C2:C3C2=--(A2)
 
Upvote 0
If you want the macro to put the result in the same column, then try the following, you will probably have to update the format of the column to Date.

VBA Code:
Sub textTodate()
  With Range("A1", Range("A" & Rows.Count).End(3))
    .Value = Evaluate("=IF({1}," & .Address & "+0)")
  End With
End Sub
 
Upvote 0
Hello Dante Amor

I am not very VBA literate.

Please educate me a little. is the 3 a shortcut for "Up" in End(3)?
and the logic of "=IF({1},"

N.B.
If the file is not macro enabled, the user could do the same thing as the macro with Paste Special ADD
copy a blank cell with Control C
select the range of Text that appear to be dates like 01 June 2020
Paste Paste Special Add
Format the numbers to your preference such as 1-Jun-20
 
Upvote 0
is the 3 a shortcut for "Up" in End(3)?
Yes it is

the logic of "=IF({1},"
check these answers

copy a blank cell with Control C
select the range of Text that appear to be dates like 01 June 2020
Paste Paste Special Add
it is an option without macro and without formula.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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