[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

How to force a column to show data as date ?

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
currentdb
Asked:
currentdb
  • 7
  • 6
  • 4
  • +1
3 Solutions
 
SiddharthRoutCommented:
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
 
jimyXCommented:
Remove the apostrophe and it will be formatted (if it does exist at the beginning).
0
 
currentdbAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
currentdbAuthor Commented:
Sid,

It works :)
Do I have to run the code each time assuming that data from column AY comes back as text ?
0
 
joeywCommented:
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
 
SiddharthRoutCommented:
Yes since you are exporting it to Excel. :(

Sid
0
 
SiddharthRoutCommented:
@joeyw: datevalue?

Sid
0
 
currentdbAuthor Commented:
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
 
joeywCommented:
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
 
SiddharthRoutCommented:
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
 
SiddharthRoutCommented:
Oops you are right joeyw.

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

Sid
0
 
joeywCommented:
I tried it just before i posted to make sure it would work with the apostrophe and it worked fine
0
 
joeywCommented:
no worries.
0
 
SiddharthRoutCommented:
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
 
currentdbAuthor Commented:
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
 
SiddharthRoutCommented:
Gr8 :)

Sid
0
 
currentdbAuthor Commented:
As always, thank you very much for your help. It's much appreciated :)
0
 
currentdbAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now