VBA Open Workbook as read only is to open an existing workbook. Workbook represents an object. It is part of workbooks collection. Open method in Excel VBA. It has multiple arguments. We use this method to open workbook file as read only when it exists in the folder. Once we know the file or workbook path exists in the folder, we open Workbook using open method. It returns the opened workbook as read only. we can’t edit anything in the opened workbook as read only file.
Syntax for Open Workbook as Read Only Method
Here is the following syntax to open Workbook as read only using VBA.
Workbooks.Open(Filename As Sring, , [ReadOnly], , , , , , , , , , , , ) As Workbook
Where FileName is a required parameter. It represents the file name of the Workbook. Remaining parameters are optional arguments.
[ReadOnly] is an optional argument. It helps to open workbook as read only file.
Macro to Open Workbook as Read Only in Excel VBA
Let us see the following example. It will open Workbook as read only from the specified location.
'VBA Open Workbook as Read Only in Excel Sub VBA_Open_Workbook_As_Read_Only() 'Variable declaration Dim sFileName As String sFileName = "D:\VBAF1\VBA Functions.xlsm" Workbooks.Open Filename:=sFileName, ReadOnly:=True End Sub
If Workbook is not available or doesn’t exists in the specified location, it throws an error. Here is the screen shot of run time error.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers