Macro to Store Date as Text

seenai

Board Regular
Joined
Mar 31, 2013
Messages
54
Hi,

I need to store the Excel DATE in selected RANGE as TEXT format. (dd/mm/yyyy).

It means the date is displayed in dd/mm/yyyy format. But it is stored as text.

Thanks in advance.

- B.Srinivasa Rao
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Let's say that you wanted to save it to cell C1. Then this code snippet would do that:
Code:
    Range("C1").NumberFormat = "@"
    Range("C1") = Format(Date, "dd/mm/yyyy")
 
Upvote 0
I have used the below code. But the dates upto 12-03-18 came in date format only. Other data is OK as Text data. :
Help me to modify the code.

Code:
Sub StoreDateasText()
Dim Cell As Range
Dim s As String
Selection.NumberFormat = "dd/mm/yyyy"
For Each Cell In Selection
        With Cell
            s = Cell.Text
            .NumberFormat = "@"
            Cell.ClearFormats
            Cell.Value = s
        End With
    Next Cell
End Sub
INPUT DATAOUTPUT DATA
01-03-1803-01-18
02-03-1803-02-18
03-03-1803-03-18
04-03-1803-04-18
05-03-1803-05-18
06-03-1803-06-18
07-03-1803-07-18
08-03-1803-08-18
09-03-1803-09-18
10-03-1803-10-18
11-03-1803-11-18
12-03-1803-12-18
13-03-1813-03-2018
14-03-1814-03-2018
15-03-1815-03-2018
16-03-1816-03-2018
17-03-1817-03-2018
18-03-1818-03-2018
19-03-1819-03-2018
20-03-1820-03-2018
21-03-1821-03-2018
22-03-1822-03-2018
23-03-1823-03-2018
24-03-1824-03-2018
25-03-1825-03-2018
26-03-1826-03-2018
27-03-1827-03-2018
28-03-1828-03-2018
29-03-1829-03-2018

<tbody>
</tbody>


Let's say that you wanted to save it to cell C1. Then this code snippet would do that:
Code:
    Range("C1").NumberFormat = "@"
    Range("C1") = Format(Date, "dd/mm/yyyy")
 
Upvote 0
Hi, Solved the problem. Thanks for support.

Code:
Sub StoreDateasText()
Dim Cell As Range
Dim s As String
Selection.NumberFormat = "dd/mm/yyyy"
For Each Cell In Selection
        With Cell
            s = Cell.Text
            .NumberFormat = "@"
            Cell.Value = s
        End With
    Next Cell
End Sub
 
Upvote 0
Yes, you were clearing the cell format right after you were setting it before (which kind of defeats the purpose of setting it in the first place!).
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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