Results 1 to 5 of 5

checking pasted/entered values for validation match, but only 1x per worksheet

Threaded View

  1. #1
    Registered User
    Join Date
    05-21-2015
    Location
    Austin, TX
    MS-Off Ver
    2013
    Posts
    20

    checking pasted/entered values for validation match, but only 1x per worksheet

    Over time I've developed this highly enforced workbook where end users can paste data and then SQL code is generated for me to input the data into our dB. The only worksheet in question here is the "data" worksheet, as well as some VBA code contained in the "ThisWorkbook" area.

    Currently the workbook is doing everything I want it to - it is correctly enforcing that the data being pasted or entered matches the Data Validation settings (and not overwriting data validation settings by changing any Paste function to a data-only paste function), and prompting the user to correct the data if it doesn't meet the criteria.

    Beyond just pasted data, I needed to implement code to also validate the data if the end user manually typed in a cell; I did that by setting up VBA to execute on any workbook change, which calls the postPasteValidation Sub and confirms that data entered matches.

    Private Sub Worksheet_Change(ByVal target As Range)
        Application.EnableEvents = False
        Application.ScreenUpdating = False
    ' LINE BELOW DUE TO PW PROMPT EVEN ON UNLOCKED DATA CELLS, WHEN WORKSHEET IS PROTECTED
        ActiveSheet.Unprotect "PW"
        Dim vCopyValue As Variant
        Dim vCopyFormula As Variant
        Dim CellAddr As String
        CellAddr = ActiveCell.Address
        On Error GoTo ExitClean
        vCopyValue = target.Value
        vCopyFormula = target.Formula
        Application.Undo
        target.Value = vCopyValue
        target.Formula = vCopyFormula
        On Error GoTo 0
    ExitClean:
        Application.CalculateFull
        Range(CellAddr).Select
    Call postPasteValidation
    ' LINE BELOW RE-PROTECTS SHEET AS VBA FINISHES AND RETURNS TO THE USER
        ActiveSheet.Protect "PW"
        Application.ScreenUpdating = True
        Application.EnableEvents = True
    End Sub
    Sub postPasteValidation()
    Call ConvertCase
    Dim validationCells As Range
    Dim oneCell As Range
    Dim userInput As Variant, promptStr As String
    On Error Resume Next
    Set validationCells = ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation)
    On Error GoTo 0
    If Not (validationCells Is Nothing) Then
        For Each oneCell In validationCells
            If Not (oneCell.Validation.Value) Then
                promptStr = oneCell.Address & " has a bad value in it." & vbCrLf _
                                & "Please enter the correct data per" & vbCrLf _
                                & oneCell.Validation.InputMessage
                userInput = Application.InputBox(prompt:=promptStr, Default:=oneCell.Value)
                If userInput <> False And oneCell.Value <> vbNullString Then oneCell.Value = userInput
            End If
        Next oneCell
    End If
    Call ConvertCase
    
    End Sub
    The issue comes up when an end user edits a single cell - such as typing a note entry in Column N "W_Notes"; when they do this, it rechecks validation on EVERY cell in the worksheet (up to my defined range of 1001 rows). That takes approximately 45 seconds, which is not really an efficient use of time to write in a note in a single cell.

    I've tried changing the setting in postPasteValidation from xlCellTypeALLValidation to xlCellTypeSameValidation, but that didn't seem to even decrease the amount of time for processing.
    Set validationCells = ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation)
    I'm OK with the "postPasteValidation" Sub running only 1 time before they close the workbook, and at that time checking all cells in the target range (and removing it from being called within the Paste and Workbook_Change Subs); but when I tried to move this code out to a Before_Close or Before_Save area, it wouldn't run.

    I've attached a blank of the workbook here; the password to unlock the "data" tab is PW . I'm open to any and all suggestions, the end users love this workbook, but not the 45 second wait any time they need to edit a cell!

    David
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Validation on Pasted Values
    By grey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2009, 12:16 PM
  2. cell validation even for values pasted into cells
    By Tom Ogilvy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 04:05 AM
  3. Replies: 0
    Last Post: 09-05-2005, 10:05 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