An implementation of Visual Basic that is built into Microsoft products.
Hi @Waseem Ahmad ,
Thanks for reaching out.
In your case, you can apply protection automatically when each new sheet is created and when the file is exported.
After the data is copied into the new sheet (DSh), you can protect it immediately by adding something like this:
DSh.Rows(1).AutoFilter
DSh.Protect Password:="YourPassword", _
AllowFiltering:=True, _
AllowSorting:=True
This allows users to:
- use filters
- navigate using arrow keys
- view the data
At the same time, it prevents editing cells or modifying the sheet.
More details about worksheet protection options are available here: https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.protect
Only (filter command & left right up down arrow) command should be enabled in all sheets
You just need to enable AutoFilter first, then protect the sheet while allowing filtering.
DSh.Rows(1).AutoFilter
DSh.Protect Password:="YourPassword", AllowFiltering:=True
You can also apply a password automatically when saving each exported workbook.
Replace your current SaveAs line with something like this:
ActiveWorkbook.SaveAs _
Filename:=ThisWorkbook.Path & "\" & ActiveSheet.Name & ".xlsb", _
FileFormat:=xlExcel12, _
Password:="YourPassword"
More information about the SaveAs parameters is available here:
https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.saveas
One thing to keep in mind is that sheet protection in Microsoft Excel mainly prevents editing rather than fully securing the data. In some cases users may still be able to copy visible cells.
Also, please treat the code snippets above as reference examples and adjust them as needed to fit your workbook or project setup.
Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.