willow1985
Well-known Member
- Joined
- Jul 24, 2019
- Messages
- 915
- Office Version
- 365
- Platform
- Windows
Run-time error 1004:
Unable to set the NumberFormat property of the Range class
I have the below code to prompt if an invalid date is entered. It also is supposed to prompt the user which format to enter, the users only understand one format so at the end I wanted the code to format it to the date format my boss prefers.
Anyway long story short I am getting the above error on the last line of the code (the formatting portion). Is there a way to fix this?
Thank you
Unable to set the NumberFormat property of the Range class
I have the below code to prompt if an invalid date is entered. It also is supposed to prompt the user which format to enter, the users only understand one format so at the end I wanted the code to format it to the date format my boss prefers.
Anyway long story short I am getting the above error on the last line of the code (the formatting portion). Is there a way to fix this?
Thank you
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B2")
If Intersect(Target, rng) Is Nothing Then
Exit Sub
Else
Call ValidateDate(rng)
End If
End Sub
Private Sub ValidateDate(r As Range)
Dim c As Range
For Each c In r
If c.Value <> "" And Not IsDate(c) Then
Application.EnableEvents = False
c.ClearContents
MsgBox "Please enter Date Returned in the following format: MM/DD/YYYY"
Application.EnableEvents = True
End If
Next c
r.NumberFormat = "[$-409]dd-mmm-yy;@"
End Sub