Solved

How to force a column to show data as date ?

Posted on 2011-03-23
18
232 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
Comment Utility
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
Comment Utility
Remove the apostrophe and it will be formatted (if it does exist at the beginning).
0
 
LVL 1

Author Comment

by:currentdb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes since you are exporting it to Excel. :(

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
@joeyw: datevalue?

Sid
0
 
LVL 1

Author Comment

by:currentdb
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
no worries.
0
 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 460 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Gr8 :)

Sid
0
 
LVL 1

Author Closing Comment

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

Author Comment

by:currentdb
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

762 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

11 Experts available now in Live!

Get 1:1 Help Now