VBA open workbook as read-only

If you want to use the VBA code to open a workbook in read-only mode, you have to follow these steps.

  1. Open an Excel workbook.
  2. Open the VBA editor by using the Alt + F11 keyboard shortcut.
  3. Enter the following code.
Sub OpenWorkbook()
    Dim my_wb As Workbook
    Dim file_path As String
    file_path = "C:\excel_file.xlsx"
    Set my_wb = Workbooks.Open(Filename:=file_path, ReadOnly:=True)
End Sub
  1. Create the file called excel_file.xlsx on the C drive (or any other path. Just remember to change the code accordingly).
  2. Run the code.
  3. Now, you have the file opened in a new window.

  1. As you can see, next to the file name there is the information that this file is opened in the read-only.