VBA - Code to Make Workbook READ-ONLY vs Edit based on drop down selection

Rana Gray

Board Regular
Joined
Jan 26, 2023
Messages
53
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hello Again!

I found an older thread with a similar question but I couldn't figure out how to apply it to my situation.

On my [Sheet1] I have a drop down box setup that has 2 options (Inquiry/Update) what I would like to do is write a script that changes the workbook from read only to editable based on the relevant selection...is this doable? I'm fine if the read-only starts from [Sheet2] onwards if that makes more logical sense.

As always thanks a bunch in advance!!!
 

Attachments

  • Image1.PNG
    Image1.PNG
    25.7 KB · Views: 5

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.
Try placing this code in the module behind the worksheet where the validation cell is located :
The code assumes the validation cell is A1... chnage this cell address via the constant declared at the top of the module.
VBA Code:
Option Explicit

Private Const VALIDATION_CELL_ADDRESS = "A1" '<== change Cell addrss to suit.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = Range(VALIDATION_CELL_ADDRESS).Address Then
        If Target = "Inquiry" Then
            ChangeWorkbookAccess ThisWorkbook, xlReadOnly
        Else
            ChangeWorkbookAccess ThisWorkbook, xlReadWrite
        End If
    End If
End Sub

Private Sub ChangeWorkbookAccess( _
    ByVal Wb As Workbook, _
    ByVal FileAccess As XlFileAccess _
)
    With Wb
        .Saved = True
        On Error Resume Next
            .ChangeFileAccess FileAccess
        On Error GoTo 0
    End With

End Sub

Note that any unsaved changes will be lost !
 
Last edited:
Upvote 0
Try placing this code in the module behind the worksheet where the validation cell is located :
The code assumes the validation cell is A1... chnage this cell address via the constant declared at the top of the module.
VBA Code:
Option Explicit

Private Const VALIDATION_CELL_ADDRESS = "A1" '<== change Cell addrss to suit.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = Range(VALIDATION_CELL_ADDRESS).Address Then
        If Target = "Inquiry" Then
            ChangeWorkbookAccess ThisWorkbook, xlReadOnly
        Else
            ChangeWorkbookAccess ThisWorkbook, xlReadWrite
        End If
    End If
End Sub

Private Sub ChangeWorkbookAccess( _
    ByVal Wb As Workbook, _
    ByVal FileAccess As XlFileAccess _
)
    With Wb
        .Saved = True
        On Error Resume Next
            .ChangeFileAccess FileAccess
        On Error GoTo 0
    End With

End Sub

Note that any unsaved changes will be lost !
Thanks so much it did work but not quite what I had in mind - which is completely my fault I guess I was going for more locked worksheets when inquiry is selected. Even in 'Inquiry' mode I was still able to make changes.
 
Upvote 0
Thanks so much it did work but not quite what I had in mind - which is completely my fault I guess I was going for more locked worksheets when inquiry is selected. Even in 'Inquiry' mode I was still able to make changes.
Hi Rana,
Well, in that case, it is easier.... Do you want this to be applied to just the worksheet where the drop down list is located or to all worksheets ?
 
Upvote 0
Hi Rana,
Well, in that case, it is easier.... Do you want this to be applied to just the worksheet where the drop down list is located or to all worksheets ?
I'm looking to lock all worksheets - let's say from [sheet2] onwards when "inquiry" mode is selected, does that make sense?
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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