PDA

View Full Version : Use VBA code in a locked worksheet



leedukes
06-01-2020, 09:08 AM
I am using Excel 365 64-bit, and am a novice with VBA. I've created an Excel file with charts in which the minimal X-axis values vary widely based on input values. I have copied and pasted VBA code that auto adjusts the vertical minimal values, but when I protect the worksheet where the graphs are located, the VBA function no longer works. Is it possible to include VBA code in a locked worksheet? Thanks!

Paul_Hossler
06-01-2020, 09:34 AM
Without seeing the macro (at least) it's hard to tell

Attach a small workbook with the chart and the macro to this thread (how to in my sig)

leedukes
06-01-2020, 10:03 AM
Without seeing the macro (at least) it's hard to tell

Attach a small workbook with the chart and the macro to this thread (how to in my sig)

Attached is the unlocked file. The charts are in the "Inputs and Summary" worksheet.

Thanks!

royUK
06-02-2020, 06:50 AM
You can unprotect a sheet, run your code then re-apply protection.



Option Explicit

Sub x()
''/// change the password here
Const PW As String = "secret"
On Error GoTo exit_proc
Sheet1.Unprotect PW
''/// your code here
exit_proc:
Sheet1.Protect PW
End Sub


The other option is to use Protect with UserInterFaceOnly

See here for an explanation and free example workbook

http://excel-it.com/excel_userinterfaceonly.html