Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Lucina Ferrrari   on Feb 18 In MS Office Category.

  
Question Answered By: Edfu Massri   on Feb 18

This should get you going I think. It alters a selection to YYYY-MM-DD.

Sub subYMD()
' Alter a selection to YYYY-MM-DD.

Dim rlSelection As Range
Dim rlCell As Range
Dim slOldDate As String
Dim slNewDate As String
Dim slAllMonths As String
Dim ilMonth As Integer
Dim slNewDateArray() As String

slAllMonths = "JANUARY" & "FEBRUARY" & "MARCH" & "APRIL" _
& "MAY" & "JUNE" & "JULY" & "AUGUST" _
& "SEPTEMBER" & "OCTOBER" & "NOVEMBER" & "DECEMBER"

Set rlSelection = Selection

For Each rlCell In rlSelection

slOldDate = rlCell.Value
slNewDateArray = Split(slOldDate)
ilMonth = InStr(slAllMonths, UCase(slNewDateArray(1)))
Select Case ilMonth
Case 1
slNewDateArray(1) = "01"
Case 8
slNewDateArray(1) = "02"
Case 16
slNewDateArray(1) = "03"
Case 21
slNewDateArray(1) = "04"
Case 26
slNewDateArray(1) = "05"
Case 29
slNewDateArray(1) = "06"
Case 33
slNewDateArray(1) = "07"
Case 37
slNewDateArray(1) = "08"
Case 43
slNewDateArray(1) = "09"
Case 52
slNewDateArray(1) = "10"
Case 59
slNewDateArray(1) = "11"
Case 67
slNewDateArray(1) = "12"
End Select
slNewDate = slNewDateArray(2) _
& "-" & slNewDateArray(1) _
& "-" & Format(slNewDateArray(0), "0#")
rlCell.Value = slNewDate

Next rlCell

End Sub

Share: 

 

This Question has 7 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Replace date with a different format in same cell? Or get search suggestion and latest updates.


Tagged: