Auto Change date format

Eric Livesay

Board Regular
Joined
Feb 13, 2008
Messages
126
I have Excel 2007 and need to be able to have a cell automatically change the entered date format from yyyymmdd (20080702) to dd-mmm-yy (2-Jul-08). I want the cell to do this whenever someone enters the date in the first format and it will then change it to the second format.

Eric
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Assuming the cell in question is A1, paste this into the sheet module...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
    Dim Yr As String, Mth As String, Day As String

    If Target.Column = 1 And Target.Row = 1 Then
        With Target
            .NumberFormat = "General"
            Yr = Left(.Value, 4)
            Mth = Mid(.Value, 5, 2)
            Day = Right(.Value, 2)
            .Value = Day & "-" & Mth & "-" & Yr
        End With
    End If

Application.EnableEvents = True


End Sub
 
Upvote 0

Eric Livesay

Board Regular
Joined
Feb 13, 2008
Messages
126
njimack: I think this will work but i need to add a little bit of info that i should have the first time. There are three cells that need this formating. The cells are labeled "start", "floor", and "grad". How do I augment your formula to reflect this?
 
Upvote 0

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
There might be a more efficient method, but this seems to work...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
    Dim Yr As String, Mth As String, Day As String

    If Target.Name = Range("Start").Name Or Target.Name = Range("Floor").Name Or Target.Name = Range("Grad").Name Then
        With Target
            .NumberFormat = "General"
            Yr = Left(.Value, 4)
            Mth = Mid(.Value, 5, 2)
            Day = Right(.Value, 2)
            .Value = Day & "-" & Mth & "-" & Yr
        End With
    End If

Application.EnableEvents = True


End Sub
 
Upvote 0

Eric Livesay

Board Regular
Joined
Feb 13, 2008
Messages
126
This doesn't work consitently. Sometimes it will stay in the yyyymmdd (20080702) format and sometimes in changes it to dd/mm/yy (02/07/08) format. Thanks for the assistance anyway.
 
Upvote 0

Forum statistics

Threads
1,191,287
Messages
5,985,762
Members
439,981
Latest member
ofori francis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top