VBA - make backup of workbook and protect cells

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
Dear all,

I have a workbook which I want to distribute among other users, but I do not want that they apply any changes to the content. So basically, it is for "read only".
I guess I have to password-protect the backup copy, and lock the cells there.
How do I write this in VBA ?

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

Use below code to protect your workbook structure and all sheets in backup copy.

VBA Code:
Sub createBackup()
Dim WB As Workbook
Dim filePath As String

'New file name contains todays date and will save in the same folder.
filePath = ThisWorkbook.Path & "\" & _
    Format(Now(), "DDMMYYHHMMSS") & "_" & _
        Left(ThisWorkbook.Name, InStr(1, ThisWorkbook.Name, ".") - 1) & ".xlsm"

'Creating backup file
ThisWorkbook.SaveCopyAs Filename:=filePath

Set WB = Workbooks.Open(filePath)
'To protect workbook structure
WB.Protect Password:="hello"


'to Protect all worksheets
For sheetnum = 1 To WB.Sheets.Count
    WB.Sheets(sheetnum).Protect Password:="pwd"
Next
End Sub
 
Upvote 0
Solution
My pleasure. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top