Lock Specific Cells while still allowing MACRO Searches, Filters and Sorting

ghp2017

Board Regular
Joined
Dec 12, 2017
Messages
51
Hello,

I have a spreadsheet that we using for inventory. To assist the counters I created buttons/macros to assist them with filtering and searching. There are a number of cells that I want to protect but when I do the buttons/macros do not work either. Does anyone know how to get around this? I can't get around the following message:

Microsoft Visual Baic

Run-time error '1004':

You cannot use this command on a protected sheet. To use this command, you must first unprotect the sheet...
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Good morning!

Unfortunately macros still get stuck like how a real person would when you lock & protect cells, lock workbooks, hide sheets, etc. Think of a macro not as an all knowing machine, but like a real person who's just doing what you want it to you (which makes your life easier.)

The way how I get around situations like this is have the macro unlock the protected sheet at the beginning of the coding, then lock it again at the end, making sure the workbook is saved after it's locked again.
The way how this might look in coding could be:

Sub example()

'You can change the 123 to whatever password you want.
Sheets(1).Unprotect Password:="123"

'
'Do whatever you want the macro to do here....
'

Sheets(1).Protect Password:="123"
Activeworkbook.Save

Hope this helps!!
 
Upvote 0
Good morning!

Unfortunately macros still get stuck like how a real person would when you lock & protect cells, lock workbooks, hide sheets, etc. Think of a macro not as an all knowing machine, but like a real person who's just doing what you want it to you (which makes your life easier.)

The way how I get around situations like this is have the macro unlock the protected sheet at the beginning of the coding, then lock it again at the end, making sure the workbook is saved after it's locked again.
The way how this might look in coding could be:

Sub example()

'You can change the 123 to whatever password you want.
Sheets(1).Unprotect Password:="123"

'
'Do whatever you want the macro to do here....
'

Sheets(1).Protect Password:="123"
Activeworkbook.Save

Hope this helps!!

This is perfect. There's just one thing. After I use a button/macro I am no longer able to use the default auto filter/drop-down list selection.
 
Upvote 0
Give this a try:

Sheets(1).Protect Password:="123", AllowFiltering:=True

But when unprotecting, still use:
Sheets(1).Unprotect Password:="123"
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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