If you want to use the VBA code to open a workbook in read-only mode, you have to follow these steps.
- Open an Excel workbook.
- Open the VBA editor by using the Alt + F11 keyboard shortcut.
- Enter the following code.
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)
- Create the file called excel_file.xlsx on the C drive (or any other path. Just remember to change the code accordingly).
- Run the code.
- Now, you have the file opened in a new window.
- As you can see, next to the file name there is the information that this file is opened in the read-only.