+ Reply to Thread
Results 1 to 6 of 6

Produce error message when pasted values break validation rules

Hybrid View

  1. #1
    Registered User
    Join Date
    04-01-2015
    Location
    Aberdeen, UK
    MS-Off Ver
    365
    Posts
    4

    Produce error message when pasted values break validation rules

    I have a (simplified example) matrix consisting of inputs and alarms. Each action (X) should have an input and an alarm i.e. no actions should be inserted in column E or row 6.

    Capture.PNG

    I used data validation to implement this and it works.

    Capture2.PNG

    However if I paste data to these cells they do not follow the validation rules. I inserted this VBA code to prevent this (extracted from www.j-walk.com/ss/excel/tips/tip98.htm):

    Private Sub Worksheet_Change(ByVal Target As Range)
        'Does the validation range still have validation?
        If HasValidation(Range("ValidationRange")) Then
            Exit Sub
        Else
           Application.EnableEvents = False
           Application.Undo
            MsgBox "Your last operation was canceled." & _
            " It would have deleted data validation rules.", vbCritical
        End If
    End Sub
    
    Private Function HasValidation(r) As Boolean
    '   Returns True if every cell in Range r uses Data Validation
        On Error Resume Next
        x = r.Validation.Type
        If Err.Number = 0 Then HasValidation = True Else HasValidation = False
    End Function
    However, this code also prevents values from being pasted into cells even when they do not break validation rules e.g. if I paste an X to input a;alarm 1, I get an error message. Is there any way to make the make the user aware if data he/she has pasted has broken data validation rules. This can be through error/warning message or highlighting of invalid cells, whichever is possible.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Produce error message when pasted values break validation rules

    Something like this?

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("ValidationRange")) Is Nothing Then
            Me.CircleInvalid
        End If
    End Sub
    This assuming you have named range ValidationRange where you apply the validation range. You can change with some range address or whatever.

    In more general terms

    Private Sub Worksheet_Change(ByVal Target As Range)
        Me.CircleInvalid
    End Sub
    Note that validation rule will be cleared if you paste from cell where there is no validation rule
    Last edited by buran; 04-01-2015 at 07:14 AM.
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Registered User
    Join Date
    04-01-2015
    Location
    Aberdeen, UK
    MS-Off Ver
    365
    Posts
    4

    Re: Produce error message when pasted values break validation rules

    Thanks for the reply.

    Yes I had ValidationRange set to D4:H8. In line with your comments I have altered the code to this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Range("D4:H8").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=AND(NOT(ISBLANK($C4)),NOT(ISBLANK(D$3)))"
            .IgnoreBlank = False
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Stop"
            .InputMessage = ""
            .ErrorMessage = "Action Must Have Input and Output"
            .ShowInput = True
            .ShowError = True
        End With
        
        Me.CircleInvalid
        
    End Sub
    This now circles all cells that have no input or alarm. But I only want cells that have an action and no input or alarm to be circled.
    To do this I know I must change the formula "=AND(NOT(ISBLANK($C4)),NOT(ISBLANK(D$3)))" but I am struggling with this. I am trying something like "=AND(ISBLANK,NOT(ISBLANK($C4)),NOT(ISBLANK(D$3)))" but this is giving an error. The answer should be pretty obvious but I can't figure it out!

  4. #4
    Registered User
    Join Date
    04-01-2015
    Location
    Aberdeen, UK
    MS-Off Ver
    365
    Posts
    4

    Re: Produce error message when pasted values break validation rules

    Okay I figured it out. Code is:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    With Range("D4:H8").Validation
            .Delete
            .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=OR(ISBLANK(D4),AND(NOT(ISBLANK($C4)),NOT(ISBLANK(D$3))))"
            .IgnoreBlank = False
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Stop"
            .InputMessage = ""
            .ErrorMessage = "Actions Must Have Input and Output"
            .ShowInput = True
            .ShowError = True
        End With
    
    Me.CircleInvalid
        
    With ActiveSheet.CircleInvalid
        Circles = ActiveSheet.Shapes.Count
            If Circles > 0 Then
                MsgBox "Actions Must Have Input and Output"
            End If
    End With
    
    End Sub
    Thanks for your help

  5. #5
    Registered User
    Join Date
    04-01-2015
    Location
    Aberdeen, UK
    MS-Off Ver
    365
    Posts
    4

    Re: Produce error message when pasted values break validation rules

    Okay I found the formula that works:

    "=OR(ISBLANK(D4),AND(NOT(ISBLANK($C4)),NOT(ISBLANK(D$3))))"

    now I just need a line of code that produces an error/message box notifying the user why a cell has been circled.

    e.g. If CircleInvalid=True
    MsgBox "Cell circled because..."

    Any ideas?

  6. #6
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Produce error message when pasted values break validation rules

    Great! If you the problem is solved, please mark thread [SOLVED]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Look at last pasted cell and if the same give me an error message box
    By jharaldson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-09-2011, 08:05 PM
  2. Validation on Pasted Values
    By grey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2009, 12:16 PM
  3. Replies: 0
    Last Post: 09-05-2005, 10:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1