Application.Undo Error

greenhillchris

New Member
Joined
Mar 5, 2022
Messages
18
Office Version
  1. 365
Hi there,

I have a worksheet that contains data vaildation via drop down menus and certain format that dates and times should be entered into cells.

I want to proetect these cells when copy and pasting from other cells and only allow the pasting if the data pasted meets the data vaildation of the cells but I also want to allow an undo function that undoes the last action taken by the user before running the macro.

I have found the below code which works how I want it to however the undo functon does not work, I have tried putting the Application.Undo code in differnet places of the code and it just closes my workbook.

Any ideas where I am going wrong with this code?


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim xValue As String
    Dim xCheck1 As String
    Dim xCheck2 As String
    Dim xRg As Range
    Dim xArrCheck1() As String
    Dim xArrCheck2() As String
    Dim xArrValue()
    Dim xCount, xJ As Integer
    Dim xBol As Boolean
'    If Target.Count > 1 Then
'        Exit Sub
'        End If
    xCount = Target.Count
    ReDim xArrCheck1(1 To xCount)
    ReDim xArrCheck2(1 To xCount)
    ReDim xArrValue(1 To xCount)
    Application.EnableEvents = False
    On Error Resume Next
    xJ = 1
    For Each xRg In Target
        xArrValue(xJ) = xRg.Value
         xArrCheck1(xJ) = xRg.Validation.InCellDropdown
        xJ = xJ + 1
    Next

    Application.Undo

    xJ = 1
    For Each xRg In Target
        xArrCheck2(xJ) = xRg.Validation.InCellDropdown
        xJ = xJ + 1
    Next

    xBol = False
    For xJ = 1 To xCount
        If xArrCheck2(xJ) <> xArrCheck1(xJ) Then
            xBol = True
            Exit For
        End If
    Next

    If xBol Then
       MsgBox "The selected cells containg data validation drop-down lists, no pasting allowed."
    Else
        xJ = 1
        For Each xRg In Target
            xRg.Value = xArrValue(xJ)
            xJ = xJ + 1
        Next

    End If

    Application.EnableEvents = True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Application.Undo doesn't work (gets lost-disabled) when editing the worksheet via code.
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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