简体   繁体   中英

VBA code for Excel cut and paste

The user can enter data in that cell:

  1. By choosing from a predefined list of options (scrolling a drop down list)

  2. By typing in directly the alphanumeric value (all numbers, all letters, or a combination of the two)

  3. By doing copy (from other documents) and paste the identifier in that cell

I do not want to give the user the right to perform option 3? Is there a VBA code to accomplish that?

Excel has always had a problem with validations. They work fine when you need someone to choose from a list or enter specific data, but the moment a user copies data from someone else and Pastes in a Validation Cell, it ignores validations and allows it

Here is an example:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Target = Range("A1") Then
        If Not (Target >= 1 And Target <= 10) Then
            Application.Undo
            MsgBox "Please enter value between 1 to 10", vbOKOnly + vbCritical
        End If
    End If
    Application.EnableEvents = True
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM