Wanting all cells that are supposed to be dates to be in MM/DD/YY format

Aaron DOJ

New Member
Joined
Aug 10, 2018
Messages
36
Office Version
  1. 365
Platform
  1. Windows
I am trying to get just the date fields in my spreadsheet to always be MM/DD/YY format no matter what kind of date is entered such as 1/1 will come out 01/01/18 or 1 jan will change to 01/01/18 etc. For the most part I got it to work with the codes below, the problem I am now encountering, that I can't seem to think how to stop is any other field with numbers or text from changing to a date format...can someone help me please :)

Worksheet
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If IsEmpty(Cells(2, 2)) Then 'if empty, put in "Value" and make value change text to teal.
    Cells(2, 2).Value = "MM/DD/YY"
    Cells(2, 2).Font.ColorIndex = 14
ElseIf Sheet1.Cells(2, 2).Value = "MM/DD/YY" Then 'if equal to "Value", change text to teal.
    Cells(2, 2).Font.ColorIndex = 14
ElseIf Sheet1.Cells(2, 2).Value <> "MM/DD/YY" Then 'if text other than "Value", change text to black.
    Call Format_Dates
    Cells(2, 2).Font.ColorIndex = 1
End If
   
If IsEmpty(Cells(3, 2)) Then 'if empty, put in "Value" and make value change text to teal.
    Cells(3, 2).Value = "MM/DD/YY"
    Cells(3, 2).Font.ColorIndex = 14
ElseIf Sheet1.Cells(3, 2).Value = "MM/DD/YY" Then 'if equal to "Value", change text to teal.
    Cells(3, 2).Font.ColorIndex = 14
ElseIf Sheet1.Cells(3, 2).Value <> "MM/DD/YY" Then 'if text other than "Value", change text to black.
    Call Format_Dates
    Cells(3, 2).Font.ColorIndex = 1
End If
   
End Sub

Module
Code:
Sub Format_Dates()
    Selection.NumberFormat = "mm/dd/yy"
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The simplest way is format cells as per the original macro and then format Y10 again

Add this line towards the end of your code (outside the formatting loop)
Code:
 Range("Y10").NumberFormat = "- mm/dd/yy"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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