+ Reply to Thread
Results 1 to 3 of 3

Prevent pasting over data validation

Hybrid View

antho27 Prevent pasting over data... 05-27-2014, 12:02 PM
AlphaFrog Re: Prevent pasting over data... 05-27-2014, 12:52 PM
antho27 Re: Prevent pasting over data... 05-27-2014, 02:54 PM
  1. #1
    Registered User
    Join Date
    11-13-2012
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    11

    Prevent pasting over data validation

    I am trying to secure a worksheet using data validation. I want to prevent users pasting over data validation fields, however I want to allow users to be able to still use the copy/paste feature as long as the data is valid.

    Attached is a sample that I found on the forums. The current code does not allow any paste functionality.

    Any help is appreciated.

    Regards,
    Anthony
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Prevent pasting over data validation

    This lets the user paste over data validation. Then it validates their pasted values and restores the data validation lists in the pasted cells.

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim cell As Range, bHasValidation As Boolean
        
        If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
        
            Application.ScreenUpdating = False
            
            For Each cell In Intersect(Target, Range("A1:A10"))
                
                On Error Resume Next
                    bHasValidation = cell.Validation.Type = xlValidateList
                On Error GoTo 0
                If Not bHasValidation Then
                
                    'Validate pasted cells
                    If Range("$F$1:$F$10").Find(cell.Value, , xlValues, xlWhole, , , False) Is Nothing Then
                        Application.EnableEvents = False
                        cell.Value = ""
                        Application.EnableEvents = True
                    End If
                    
                    'Restore validation lists
                    With cell.Validation
                        .Add Type:=xlValidateList, _
                             AlertStyle:=xlValidAlertStop, _
                             Operator:=xlBetween, _
                             Formula1:="=$F$1:$F$10"
                        .IgnoreBlank = True
                        .InCellDropdown = True
                        .InputTitle = ""
                        .ErrorTitle = ""
                        .InputMessage = ""
                        .ErrorMessage = ""
                        .ShowInput = True
                        .ShowError = True
                    End With
                End If
            Next cell
            
            Application.ScreenUpdating = True
            
        End If
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    11-13-2012
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    11

    Re: Prevent pasting over data validation

    wow thank you!! This is great.

    Is there a way to add a message box if the data is not valid? or prevent pasting special values?

+ 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. Prevent users from changing formatting when pasting in data.
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2014, 05:03 PM
  2. Unable to prevent copy pasting data that does not conform to data validation rules
    By Sree Varun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-25-2013, 10:41 PM
  3. Prevent copying and pasting a cell from a column with data validation to another column
    By kieranoduill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2013, 10:02 AM
  4. Prevent pasting over validation
    By BuzzT in forum Excel General
    Replies: 1
    Last Post: 02-07-2011, 04:51 PM
  5. Is there a way to prevent pasting data into an Excel worksheet?
    By Rod from Corrections in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 08-04-2006, 10:20 PM

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