Return Original Cell Value Is User Value Entered Is Invalid

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a cell on my worksheet that gets autopopulated with a value. The user can access the cell and change the value (a date).
I'm looking for some help in error checking. The value entered can only be a date, and it can only be a Friday. If it isn't, I'd like the cell to return to it's original value. Is this possible. I want to think it has something to do with "Cancel" but I don't know how touse it in this situation.

Here is my code ...
VBA Code:
Private Sub worksheet_change(ByVal Target As Range)
    If Target.Address = "$C$4" Then
        uidate = Target.Value 'user inpute date
        If Weekday(uidate) <> 6 Then 'date entered isn't a friday
            'can only be a friday. Return original value to cell C4
        End If
    End If
End Sub

Is anyone able to help me find a solution to return the original value to D4. I think I can use "isdate" to determine if the value the user entered is indeed a date vs. text or some redundant entry.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I think something like this should work...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim uidate As Variant
    Application.EnableEvents = False
    On Error GoTo Whoops
    If Target.Address = "$C$4" Then
        uidate = Target.Value
        If Weekday(uidate) <> 6 Then
            Application.Undo
        End If
    End If
    Application.EnableEvents = True
    Exit Sub
Whoops:
    Application.Undo
    Application.EnableEvents = True
End Sub
 
Upvote 1
Solution
Thank you Rick. I'm grateful for your help.
This works wonderfully.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top