VBA Open Workbook as Read Only

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.
RunTime Error

Instructions to use Macro

Here are the instructions to use above macro procedure in Visual basic editor.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can see output on the screen
  • Find above output screenshot of the specified procedure.

Related Workbook Object articles

You may also like the related Workbook Object articles.

VBA Workbook Object

Leave a Comment

Your email address will not be published. Required fields are marked *