Auto Change date format

Eric Livesay

Board Regular
Joined
Feb 13, 2008
Messages
127
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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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
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
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
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,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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
Back
Top