Forum

Wsheet Change Event...
 
Notifications
Clear all

[Solved] Wsheet Change Event Date Format

3 Posts
2 Users
1 Reactions
360 Views
(@blackvan)
Posts: 30
Trusted Member
Topic starter
 

I'm trying to create A Wsheet change event that will change the format of a date entered in col D from dd/mm/yy to dd.mm

My code:

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("D:D")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Target.HasFormula Then
Target.NumberFormat = "dd.mm"
Application.EnableEvents = True
End If

End Sub

However, when I test it nothing happens. What am I missing?

Thanks!

 
Posted : 22/12/2024 1:31 am
(@walterpa)
Posts: 1
New Member
 

Your code is close, but there are a few issues preventing it from working as intended. Here's a revised version. Place this in the Worksheet you are editing.

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D:D")) Is Nothing Or Target.Cells.Count > 1 Then
Exit Sub
End If

Application.EnableEvents = False
On Error GoTo ErrorHandler

If Not Target.HasFormula Then
If IsDate(Target.Value) Then
Target.NumberFormat = "dd.mm"
Target.Value = Target.Value ' Trigger recalculation
End If
End If

ExitSub:
Application.EnableEvents = True
Exit Sub

ErrorHandler:
MsgBox "An error occurred: " & Err.Description
Resume ExitSub
End Sub

 
Posted : 22/12/2024 10:45 am
(@blackvan)
Posts: 30
Trusted Member
Topic starter
 

Thanks mate!

 
Posted : 22/12/2024 9:18 pm
Share: