Solved

vba that will change excel date to text date

Posted on 2014-07-22
9
1,784 Views
Last Modified: 2014-08-08
is it possible to change the cell format in excel from Date to text so that 22/07/2014 will become 27Jul14 instead of 41842 using VBA code.

Thanks
0
Comment
Question by:Jagwarman
  • 4
  • 4
9 Comments
 
LVL 22

Expert Comment

by:Flyster
ID: 40213012
This will give you the formatting you want. Just change the range to what you need:

Sub FormatCell()
    Range("A1").Select
    Selection.NumberFormat = "ddmmmyy"
End Sub

Flyster
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40213016
This code converts the format for the active (selected cell):
Sub Convert_Date()
    With ActiveCell
        .NumberFormat = "@"
        ActiveCell.Value = Day(ActiveCell.Value) & _
            Format(ActiveCell.Value, "mmm") & _
            Format(ActiveCell.Value, "yy")
    End With
End Sub

Open in new window


-Glenn
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40213019
Flyster's code preserves the internal date (41842); mine converts it to text.
0
 

Author Comment

by:Jagwarman
ID: 40213511
Glenn is correct Flyster, I needed to change it to text but thanks for trying.

Glenn that is exactly what I am looking for many thanks
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Jagwarman
ID: 40213560
GlennRay is it possible for this to work over a range i.e. F:F or F:G I presumed I could change ActiveCell to ActiveRange but nothing is ever simple.

Thanks
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40214651
Sorry for the late reply, but yes, here's modified code to run over all values in a single column (in this example, column F):
 
Sub Convert_Dates()
    Dim rng As Range
    Dim cl As Object
    Set rng = Range("F2:F" & Cells.SpecialCells(xlLastCell).Row)
    For Each cl In rng
        If cl.Value <> "" Then
            With cl
            .NumberFormat = "@"
            cl.Value = Day(cl.Value) & _
                Format(cl.Value, "mmm") & _
                Format(cl.Value, "yy")
            End With
        End If
    Next cl
End Sub

Open in new window


You could change the column in line 4 in the Range("F2:F" portion, even expand it to column G as you suggested by changing the second "F" in that part.

-Glenn
0
 

Author Comment

by:Jagwarman
ID: 40215478
Thanks Glenn
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40215531
You're welcome.
0
 

Author Comment

by:Jagwarman
ID: 40248434
Glenn, hope you will pick this up. Your code works great but i need to have the date format like 01AUG2014,  08AUG2014 and not 1AUG2014, 8AUG2014

Can you help?
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 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

10 Experts available now in Live!

Get 1:1 Help Now