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.
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.
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.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.