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

Rana Gray

Board Regular
Joined
Jan 26, 2023
Messages
56
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: 8

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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