Solved

How to force a column to show data as date ?

Posted on 2011-03-23
18
234 Views
Last Modified: 2012-05-11
Dear experts,

How I can force a column to show its contents as date instead of text ?

For now the column is formatted as ''date'', but the date is shown as '2011-03-22 instead of 2011-03-22

I tried to format the column via column properties, but nothing changed.

Any help would be appreciated.

Thanks
currentdb
0
Comment
Question by:currentdb
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 460 total points
ID: 35198990
Run this code from a module. I am assuming that the dates are in Col A

Sub Sample()
    Dim i As Long, LastRow As Long
    
    LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To LastRow
        Range("A" & i).Value = Replace(Range("A" & i).Value, "'", "")
    Next i
End Sub

Open in new window


Sid
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35198995
Remove the apostrophe and it will be formatted (if it does exist at the beginning).
0
 
LVL 1

Author Comment

by:currentdb
ID: 35199010
The dates are in Col AY and that's the worst nightmare. I'll run your code now and be back.

jimyX: I can do that, but I won't do that for 5000 rows of data...:(
0
 
LVL 1

Author Comment

by:currentdb
ID: 35199090
Sid,

It works :)
Do I have to run the code each time assuming that data from column AY comes back as text ?
0
 
LVL 4

Assisted Solution

by:joeyw
joeyw earned 40 total points
ID: 35199097
insert a new column next to AY and add the formula

=datevalue(AY2)     (assuming data starts in row 2)

copy the formula to the last row (double click the little square at the bottom right corner of the cell)
copy the new column
paste special and choose values to copy the data back into column AY
delete the new column
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35199098
Yes since you are exporting it to Excel. :(

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35199114
@joeyw: datevalue?

Sid
0
 
LVL 1

Author Comment

by:currentdb
ID: 35199144
Yes since you are exporting it to Excel. :( ---> Instead of running the code manually, is there a possibility to call it from a macro? assuming that this macro executes itself on open (when the worksheet is opened).

0
 
LVL 4

Expert Comment

by:joeyw
ID: 35199145
converts a string formatted as a date to a date.  The ' makes the cell value a string.

However, if this is a spreadsheet where the data is recreated regularly, I would recommend the creating the module in a workbook (I create a workbook called Useful macros) and then opening the macro workbook and the data workbook at the same time.  You can then access the macros from you data workbook to reformat this everytime (assuming the data always stays in column AY.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35199155
joeyw:

I mean datevalue won't work for date stored as '2/12/2010

It will only work with =DATEVALUE("8/22/2010")

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35199166
Oops you are right joeyw.

I was testing it as =DATEVALUE("'2/12/2010") and not as =datevalue(AY2)

Sid
0
 
LVL 4

Expert Comment

by:joeyw
ID: 35199170
I tried it just before i posted to make sure it would work with the apostrophe and it worked fine
0
 
LVL 4

Expert Comment

by:joeyw
ID: 35199180
no worries.
0
 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 460 total points
ID: 35199199
currentdb: Paste this code in the workbook open event.

Private Sub Workbook_Open()
    Dim i As Long, LastRow As Long
    
    LastRow = Sheets("Sheet1").Range("AY" & Rows.Count).End(xlUp).Row
    
    For i = 1 To LastRow
        Range("AY" & i).Value = Replace(Range("AY" & i).Value, "'", "")
    Next i
End Sub

Open in new window


Sid
0
 
LVL 1

Author Comment

by:currentdb
ID: 35199262
Sid, looks like it works! ;) It even works on the other sheet linked to a Ms-Access table. Finally a nightmare comes to an end for this (I hope so ) :)
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35199274
Gr8 :)

Sid
0
 
LVL 1

Author Closing Comment

by:currentdb
ID: 35199346
As always, thank you very much for your help. It's much appreciated :)
0
 
LVL 1

Author Comment

by:currentdb
ID: 35199424
Sid, just opened another question here (an easy one for you of course) : http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26906242.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now