Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change Date format to (dd-mmm-yy) if the inserted values are Date , Not Work?

I need Change Date format to (dd-mmm-yy) if the inserted values are Date ,But it is Not Work !

the current result format is dd-mm-yy and the expected is dd-mmm-yy.
I tried the below code. as always,any help will be appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)

  If Not Intersect(Target, Columns("E:F")) Is Nothing Then  'Change Date format to dd-mmm-yy
  
        If Not IsDate(Target.value) = False Then
         
         Target.NumberFormat = "dd-mmm-yy"
         
         End If
       End If

End Sub

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You probably need to loop, if you’re changing multiple cells at once. Note that changing the number format doesn’t change the underlying value. If you’re working with text-that-looks-like-a-date, changing the number format won’t do anything. You’d need to convert it to a date first (maybe using CDate or DateValue).

Dim rng As Range
Set rng = Intersect(Target, Columns("E:F"))

If Not rng Is Nothing Then
   Dim cell As Range
   For Each cell In rng
       If IsDate(cell.Value) Then
           cell.NumberFormat = "dd-mmm-yy"
       End If
   Next
End If
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading