0
votes

How can I know which cells a Pasted or Copy/Pasted cells contains.

I want to let the user copy and paste cells when the copied range contains any of the cells from the first column Column A, If not then return a msgbox error (but I dont know how to do this with the Worksheet Change event.

Expected Results:

When copy/Pasted does not contains at least one cell from Column A (FirstColumn) then returns a error, but if contains one from columnA than copy/pasted is allowed (how can i achieve this)?

1
Do you have any code that you can share with us?BigBen
Unfortunately, excel does not provide a 'before worksheet change' event, only an 'on worksheet changed' event. So, while it is possible to determine that a user has changed a cell in column A, it is not possible to intercept the paste event and prevent column A from being changed. I suggest you look into 'locked cells' and worksheet security to guarantee that a user can't change particular regions of a worksheet.Ryan B.
Its not possible like when he paste (you can probably know what got pasted (by saying this i mean what was the cells/range he pasted to determine if got column A on pasted cells)? And if the paste does not contains the conditions you want i was thinking just doing and application.undo + msgbox(error...) ?mrluffy
sure, you can do that. Use the INTERSECT method to see if the TARGET range overlaps with column A.Ryan B.
@RyanB. It is possible actually, I'll find code to share for it.Riley Carney

1 Answers

1
votes

you could try this, assuming the user must select the data before copy.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim a As Long
a = Selection.Cells.Count
If Intersect(Target, Range("A:A")) Is Nothing Then
        MsgBox ("error you must copy data from 'A' column")
else 
MsgBox "you have selected " & (a) & "cells" 
End If
End Sub

place it on a worksheet object of your vba project where is stored the data to copy
hope this helps