Solved

How to force a column to show data as date ?

Posted on 2011-03-23
18
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

730 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