Date Formatting (don't put slashes, but get with slashes)

Sahak

Well-known Member
Joined
Nov 10, 2006
Messages
1,008
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
I all,
I would like to have the cell formatted MM/DD/YYYY.
Is it possible to format or to use VBA, if I in the cell will enter for example 040512 (no slashes), after finishing the entry, it will show 04/05/2012, if I enter 100421 (no slashes), after finishing the entry, it will show 10/04/2021.
Thank you in advance
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
VBA Code:
strTest = Range("A1").Value
msgbox datevalue(left(strtest,2) & "/" & mid(strtest,3,2) & "/" & right(strtest,2))
 
Upvote 0
VBA Code:
strTest = Range("A1").Value
msgbox datevalue(left(strtest,2) & "/" & mid(strtest,3,2) & "/" & right(strtest,2))
Thank you for your reply,
Sorry for the misunderstanding, I don't want to see it on Message Box, I need to have formatted it in the cell, I'm entering numbers in the cell without slashes but in the same cell it getting formatted with slashes.
 
Upvote 0
Ok then,
This will work for you:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  With Target
    Application.EnableEvents = False
    .Value = Format(CDate(Mid(.Value, 3, 2) & "/" & Left(.Value, 2) & "/" & Right(.Value, 2)), "MM/dd/yyyy")
    Application.EnableEvents = True
  End With
End Sub
 
Upvote 0
try to set the cell custom number format => 00"/"00"/"00
or => 00!/00!/00

refer to
 
Last edited:
Upvote 0
Yes, (y)(y)
00"/"00"/20"00;@
works
Thank you all very much
 
Upvote 0
Hmm, does that mean that you are not using those dates for any calculations, lookups, comparisons etc?

For example, in the sheet below I have entered 020104 in cell A1 and I have entered the date 02/01/2004 in B1. the formulas in c1, D1 show that they are not the same and in fact as dates would be 17,884 days apart. In fact, if you hover over A1 in my mini sheet the pop-up shows 20104 (which is what I entered but dropped the leading 0) and if you hover over B1 it shows 37988. (You may get different values to what I have just quoted as my date system is d/m/y order, but the concept is just the same)

If it is just a visual thing and you don't care about the underlying actual date, then no problem but if you are going to use that date for something else then there will be problems.

Sahak.xlsm
ABCD
102/01/200402/01/2004FALSE17884
Sheet1
Cell Formulas
RangeFormula
C1C1=A1=B1
D1D1=B1-A1
 
Upvote 0
Thank you Peter very much, good catch. it is good to know. It gives a problem with sorting too:unsure:
 
Upvote 0

Forum statistics

Threads
1,215,603
Messages
6,125,788
Members
449,260
Latest member
Mrw1

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