Mar 27 2021 11:44 PM
I have some macros on a sheet that is protected as it's set up like a user form, which I have protected so it doesn't get messed up by the various users. I've unlocked the cells they can modify, and protected the sheet to allow for other users to select unlocked cells. I created the macro based on recording my keystrokes via Excel's Macro Recorder, and it works fine if I don't protect the worksheet at all. Once I added the protection, it always hangs up at the Selection.PasteSpecial line.
'Unprotect the worksheet
ActiveSheet.Unprotect
AllowFormattingCells = True
'
Range("AR5").Select
Selection.Copy
Range("G5:AC21").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Mar 28 2021 01:07 AM
You can unprotect the worksheet, perform actions that are only allowed in an unprotected sheet, then protect the sheet again:
'Unprotect the worksheet
ActiveSheet.Unprotect
' Copy and paste formulas
Range("AR5").Copy
Range("G5:AC21").PasteSpecial Paste:=xlPasteFormulas
' Protect the sheet again
ActiveSheet.Protect AllowFormattingCells:=True
Mar 28 2021 04:17 AM - edited Mar 28 2021 04:26 AM
I believe @HansVogelaar has provided the solution to your problem, but you might like to take a further step beyond macro recorder. Unless it is important to you to transfer the font colour, size, cell colour, conditional formats, protection etc., you could simply transfer the values (or, I think in this case, the formula)
'Unprotect the worksheet
ActiveSheet.Unprotect
'Copy formulas
Range("G5:AC21") = Range("AR5").FormulaR1C1
' Protect the sheet again
ActiveSheet.Protect AllowFormattingCells:=True
Personally, I would also use defined Names, just in case you wish to extend the Range "G5:AC21" or someone moves the cell "AR5". It is very easy to forget to adjust the VBA otherwise.
Mar 28 2021 10:53 AM
Mar 28 2021 10:55 AM
Mar 28 2021 11:22 AM
Mar 28 2021 11:52 AM
I would have expected your code to stop at
AllowFormattingCells = True
since that isn't valid by itself.
Mar 28 2021 12:17 PM
Mar 28 2021 12:29 PM
Have you tried the code posted by Peter Bartholomew or by me?
Apr 09 2021 12:18 PM
I was finally able to get the code working - for anyone interested, I left the unprotect/protect code out of all the macros and only put it on the main VBA code for the workbook, and now it works great. Thanks all for your help!
Feb 03 2024 10:41 PM
@Cyndie_Birdsong
I see that you resolved your issue, but I would like to add information to your solution:
Maybe this is an official description of your issue.
VBA code "behind" a worksheet or a workbook may not work in Excel - Microsoft Support