Solved

How to force a column to show data as date ?

Posted on 2011-03-23
18
236 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

803 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