Forum Discussion
Sharon_Dquest1
Mar 11, 2021Copper Contributor
Automatically converting MO/00/YR to the last day of the month
Hi, I work in a lab where we use excel 365 to document out quality control. Some of the reagents we use have the expiration as MO/year. I want to know if there is a way for us to put MO/00/yr and...
HansVogelaar
Mar 11, 2021MVP
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim m As Long
Dim y As Long
If Not Intersect(Range("E4:E10000"), Target) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each c In Intersect(Range("E4:E10000"), Target)
If Mid(c.Value, 3, 4) = "/00/" Then
m = Left(c.Value, 2)
y = 2000 + Right(c.Value, 2)
c.Value = DateSerial(y, m + 1, 0)
End If
Next c
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
Switch back to Excel.
Save the workbook as a macro-enabled workbook.
Make sure that you allow macros when you open it.
Sharon_Dquest1
Mar 11, 2021Copper Contributor
That works. Thanks so much!!
- Sharon_Dquest1Mar 11, 2021Copper ContributorWell, it was working...
- HansVogelaarMar 11, 2021MVP
Not anymore?
- Sharon_Dquest1Mar 17, 2021Copper ContributorI tried figuring out how to do multiple columns using that code by myself but I can't get it. So in this scenario we have multiple reagents in the same worksheet that have an expiration date with the 12/00/21 format. Can I do that? Thanks,