Solved

vba that will change excel date to text date

Posted on 2014-07-22
9
1,828 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

910 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

25 Experts available now in Live!

Get 1:1 Help Now