Reputation: 1
I'm trying to figure out how to hide locked rows using VBA code. Let's say I have 3 password protected worksheets, with all cells locked and UserInterfaceOnly:=True.
My ActiveX button is on Sheet1. I want to hide Rows("1:2") in all of the Worksheets (Sheet1, Sheet2 and Sheet3). However, the code works fine just for the Sheet the button is on, Sheet1. When it comes to hiding rows on Sheet2/Sheet3, Excel says that I can't hide that range because it is locked. However, it's locked on Sheet1 too, but it simply works and hides/unhides rows 1:2.
I've already made a solution, unprotecting those 2 other sheets right before hiding/unhiding the rows and then protecting them again. This, however, doesn't look OK to me and makes the Excel go between all those sheets which takes some (very little) time and "flashes" when doing so. This is unwanted for me. The code looks like this:
Rows("1:2").EntireRow.Hidden = True
Sheet2.Unprotect Password:=pwd
Sheet2.Rows("1:2").EntireRow.Hidden = True
Sheet2.Protect Password:=pwd, AllowFormattingCells:=True, UserInterfaceOnly:=True
Sheet3.Unprotect Password:=pwd
Sheet3.Rows("1:2").EntireRow.Hidden = True
Sheet3.Protect Password:=pwd, AllowFormattingCells:=True, UserInterfaceOnly:=True
As you can see, I'm not unprotecting Sheet1 and it works.
Is there any way to achieve this or would it always work only on the worksheet which the ActiveX button is on? Also, I don't want to use AllowFormatingRows:=True.
Thanks
Upvotes: 0
Views: 1358
Reputation: 3254
To prevent the screen flashing, you can set Application.ScreenUpdating = False
before running your macro, and return it to True
afterwards.
As for being able to hide rows without unprotecting the sheet they are on first, I don't think that is possible. Frankly I am surprised it worked on the ActiveSheet
while that was protected in the first place.
Finally, and this is just me nitpicking, I would qualify what sheet that first line of code works on as well - being explicit with what objects you work on is a good way to prevent unexpected behavior.
Upvotes: 2
Reputation: 21
I'm affraid, that you should use Unprotect/Protect commands. It will not work without that.
But, you can try to use Application.ScreenUpdating flag to remove the "flashes".
Application.ScreenUpdating = False
Rows("1:2").EntireRow.Hidden = True
Sheet2.Unprotect Password:=pwd
Sheet2.Rows("1:2").EntireRow.Hidden = True
Sheet2.Protect Password:=pwd, AllowFormattingCells:=True, UserInterfaceOnly:=True
Sheet3.Unprotect Password:=pwd
Sheet3.Rows("1:2").EntireRow.Hidden = True
Sheet3.Protect Password:=pwd, AllowFormattingCells:=True, UserInterfaceOnly:=True
Application.ScreenUpdating = True
Upvotes: 2