vba excel data validation - how to clear entry?

plhuff

New Member
Joined
May 13, 2016
Messages
2
I get the input and error title I want on invalid Excel dates, but clicking 'Ok' still leaves the invalid entry. Clicking 'Cancel' clears it.
Is my syntax wrong? Can I refer to the cell in error and clearcontents on it?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim lValue As Boolean
Dim lRow As Long, lDVTYpe As Long
Dim sTemp As Shape
Dim ws As Worksheet
Dim rCell As Range, c As Range
Dim sMyString As String
Application.EnableEvents = False
Set ws = ActiveSheet
lRow = Cells(Rows.Count, "A").End(xlUp).Row
'We set our range variable


Set rCell = ActiveSheet.Range("AZ2:AZ" & lRow)


On Error Resume Next
lDVTYpe = 4
lDVTYpe = Target.Validation.Type
On Error GoTo errHandler


With Range("AZ2:AZ" & lRow).Validation
.Modify Type:=xlValidateDate, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="01/01/1900", Formula2:="12/31/2600"
.InputTitle = "Input"
.ErrorTitle = "Enter "
.InputMessage = " mm/dd/yyyy"
.ErrorMessage = " mm/dd/yyyy"
.IgnoreBlank = True
 MsgBox ("At1 " & rCell.Address & " " & rCell.Value)
End With
errHandler:
  MsgBox ("Error Handler")
  MsgBox (Target.Address)
  Application.EnableEvents = True
  Exit Sub
End Sub
/CODE

phuff
 
This will clear the cell(s) that you have selected.

Code:
Target.ClearContents

This will clear cells(s) that have formulas that result in errors:
Code:
[COLOR=#0000ff]    On Error Resume Next
[/COLOR][COLOR=#008000]    '16 signifies the constant for xlErrors[/COLOR]
    Target.SpecialCells(xlCellTypeFormulas, 16).ClearContents
[COLOR=#0000ff]    On Error GoTo 0[/COLOR]
 
Last edited:
Upvote 0

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