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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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