This seems like it should be a simple problem but I’ve been unable to solve it. I have a program I’m writing where the user is babied through these steps:
Step 1: Go to another spreadsheet and copy the content
Step 2: Come back to my spreadsheet and press a button to paste that content
When they press my button it needs to unlock the current sheet and paste the data without any of the formatting. This is what I have:
ActiveWorkbook.ActiveSheet.Unprotect
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
So it works perfectly if I don’t do the “Unprotect” line, but I’m trying to stop people from pasting the data without using my button.
I know that the issue is something to do with clipboard content being lost when I unprotect the sheet but the methods I’ve used to retain that content aren’t working, namely:
- Lib User32 – OpenClipboard and CloseClipboard
- DataObject – GetFromClipboard, PutInClipboard
Curiously, it works perfectly if I debug the VBA and step through line by line?!?
Okay, I’ve solved it (but someone may have a better solution). I believe the problem was that when I used the DataObject method of keeping the clipboard contents it stripped the formatting and so it couldn’t PasteSpecial, that’s actually fine with me because I’m only after the data anyway! With that in mind, this seems to work: