Allow VBA to modify cells in protected sheet

eromilly

New Member
Joined
Apr 13, 2011
Messages
3
I wrote a workbook and want protect the sheets from "people edit", but still allow vba code to update cell values and sort ranges, etc. Is this possible?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Sure, just unprotect and protect the sheet around your code; something like:

ActiveSheet.Unprotect
' your vba code here
ActiveSheet.Protect
 
Upvote 0
There are 2 ways...

1. Unprotect Sheet - run code - reprotect sheet

Sheets("Sheet1").Unprotect "passwordgoeshere"
'Do stuff
Sheets("Sheet1").Protect "passwordgoeshere"


2. UserInterfaceOnly option

Sheets("Sheet1").Protect Password:="passwordgoeshere", userinterfaceonly:=True



I prefer the 1st method because the userinterfaceonly option is NOT remembered upon save/close/reopen.
You have to reset that option each time the book is opened.
 
Upvote 0
Am I doing this right?

I checked it on a co-workers computer and it worked after she clicked on the enable editing pop up

But they went back in and it did not update.

Function GetUserName()
Sheets("Sheet1").Unprotect "123"
GetUserName = Application.UserName
Sheets("Sheet1").Protect "123"
End Function

Sorry I know its been dead 6 years :(
 
Upvote 0
So just this should work?

Function GetUserName()
GetUserName = Application.UserName
End Function

Even with the cell being locked and the sheet being password protected?
 
Upvote 0
Which cell ?

Are you putting that in a cell as a formula, like in A1 you put =GetUserName()
?

In that case, the sheet needs to be unprotected by hand, then enter the formula in the cell, then reprotect the sheet.
 
Upvote 0
You could use a separate macro to do it (not contained within your custom function)

Code:
Sub EnterFormula()
Sheets("Sheet1").Unprotect "123"
Sheets("Sheet1").Range("A1").Formula = "=GetUserName()"
Sheets("Sheet1").Protect "123"
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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