Disable cut/copy/paste only on certain ranges?

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
The below code disables cut/copy/paste in a worksheet

Code:
Private Sub worksheet_activate()
  EnableControl 21, False   ' cut
  EnableControl 19, False   ' copy
  EnableControl 22, False   ' paste
  EnableControl 755, False  ' pastespecial
  Application.OnKey "^c", ""
  Application.OnKey "^v", ""
  Application.OnKey "+{DEL}", ""
  Application.OnKey "+{INSERT}", ""
  Application.CellDragAndDrop = False
End Sub

Private Sub worksheet_deactivate()
  EnableControl 21, True   ' cut
  EnableControl 19, True   ' copy
  EnableControl 22, True   ' paste
  EnableControl 755, True  ' pastespecial
  Application.OnKey "^c"
  Application.OnKey "^v"
  Application.OnKey "+{DEL}"
  Application.OnKey "+{INSERT}"
  Application.CellDragAndDrop = True
End Sub

Is there a way to make it only work when a certain range is selected, e.g. range(a1:a10")? In short users should be able to cut copy and paste except only on a1:a10?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Why don't you just lock the cells in question?
I have a dropdown list (data validation) in some of the cells. Locking them would mean that the values can not be changed, even if hte values being entered are within the dropdown list.
 
Upvote 0
I saw this post earlier and was hoping that somebody would come up with something better than what I was determining. Maybe there isn't an easy way to accomplish what you are looking for? Even if we were to utilize code to protect your range, we would only be able to react after the fact. For example, if we were to run the code located within your worksheet_activate event when some cell or cells were selected in the range a1:a10, it would be after a paste or drag operation. Not knowing the details of what validation or level of protection you are using in range a1:a10, it is pretty much impossible to give you the most basic example. I think you should start over by defining for potential responders what constraints you wish to place on this range. There may be a better method available or a more logical approach than disabling copying, pasting, dragging, and dropping. I have used mirroring in times past to return a range to it's previous values if validation failed. This is, in effect, creating a mirror copy of some range on a hidden worksheet. If the user makes invalid changes, this copy is used to restore the range to it's previous values, formatting, ect... However, this may be overkill and that is why I have hesitated to post an example. Please provide some more details about your range and any other relevant details about your project...
 
Upvote 0
Hi there

Another idea where the area is small is to take a picture of it, paste it over the top and protect (object only). Select the area, hold down shift and select Edit / Copy Picture / As shown on Screen / OK then Edit /Paste

regards
Derek
 
Upvote 0
As I mentioned in your other post on the same general subject, I *hate* making the consumer's life more difficult just to make the programmer's life easy. This is no exception.

Some weeks back, I developed a "super data form" add-in that I haven't been able to share on my web site. In it, I essentially let the user do whatever they want and then "reimpose" all the validation criteria just in case someone somehow pasted something that deleted the old formatting / validation.

While the add-in solves the general UI problem for any data set, a simplified version you may want to consider would be triggered by the worksheet's change event. Duplicate the validation criteria for the cell in which data will be entered in some other protected cell. Now, use the change event to see if the cell with d.v. has been changed. If so, reimpose the existing d.v. and validate the cell. In the example I used, D7 contained a copy of the validation criteria for E7.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Application.Intersect(Target, Range("e7")) Is Nothing Then Exit Sub
    Range("d7").Copy
    Application.EnableEvents = False
    On Error GoTo Catch1
    Target.PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    If Not Target.Validation.Value Then
        MsgBox "Oops! That's not a valid value." & vbNewLine _
            & "Please select from the drop down list"
        Target.ClearContents
        Target.Activate
        End If
    GoTo Finally1
Catch1:
    Resume Finally1
Finally1:
    Application.EnableEvents = True
    End Sub
The below code disables cut/copy/paste in a worksheet

Code:
Private Sub worksheet_activate()
  EnableControl 21, False   ' cut
  EnableControl 19, False   ' copy
  EnableControl 22, False   ' paste
  EnableControl 755, False  ' pastespecial
  Application.OnKey "^c", ""
  Application.OnKey "^v", ""
  Application.OnKey "+{DEL}", ""
  Application.OnKey "+{INSERT}", ""
  Application.CellDragAndDrop = False
End Sub

Private Sub worksheet_deactivate()
  EnableControl 21, True   ' cut
  EnableControl 19, True   ' copy
  EnableControl 22, True   ' paste
  EnableControl 755, True  ' pastespecial
  Application.OnKey "^c"
  Application.OnKey "^v"
  Application.OnKey "+{DEL}"
  Application.OnKey "+{INSERT}"
  Application.CellDragAndDrop = True
End Sub

Is there a way to make it only work when a certain range is selected, e.g. range(a1:a10")? In short users should be able to cut copy and paste except only on a1:a10?
 
Upvote 0
I saw this post earlier and was hoping that somebody would come up with something better than what I was determining. Maybe there isn't an easy way to accomplish what you are looking for? Even if we were to utilize code to protect your range, we would only be able to react after the fact. For example, if we were to run the code located within your worksheet_activate event when some cell or cells were selected in the range a1:a10, it would be after a paste or drag operation. Not knowing the details of what validation or level of protection you are using in range a1:a10, it is pretty much impossible to give you the most basic example. I think you should start over by defining for potential responders what constraints you wish to place on this range. There may be a better method available or a more logical approach than disabling copying, pasting, dragging, and dropping. I have used mirroring in times past to return a range to it's previous values if validation failed. This is, in effect, creating a mirror copy of some range on a hidden worksheet. If the user makes invalid changes, this copy is used to restore the range to it's previous values, formatting, ect... However, this may be overkill and that is why I have hesitated to post an example. Please provide some more details about your range and any other relevant details about your project...

Hi,

My worksheet has dropdown list in one of the column. I want the users to strictly select from the dropdown list because it is very important that the values entered are valid. However some of them simply copy and paste into the column, thereby overwriting the data validation and worse entering an invalid value.

Locking the cells and protecting the sheet is not an option because if I do that, the users will not be able to select form the dropdown list.

That is why my only option is to disable pasting when a range in the sheet is selected.

Regards
 
Upvote 0
{snip}
That is why my only option is to disable pasting when a range in the sheet is selected.

Regards
No, it isn't. I gave you a *working* solution that provides you with the data integrity you want and your customers with the flexibility of using the UI to its fullest capability.
 
Upvote 0
{snip}
That is why my only option is to disable pasting when a range in the sheet is selected.

Regards
No, it isn't. I gave you a *working* solution that provides you with the data integrity you want and your customers with the flexibility of using the UI to its fullest capability.

Hi Tusharm,

Yes I am looking into your suggestion now...

Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,943
Messages
6,127,820
Members
449,409
Latest member
katiecolorado

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