VinodKrishnappa
New Member
- Joined
- Aug 6, 2016
- Messages
- 31
Hi,
I'm new to this macro. I found following macro to protect sheet in a folder. But somehow it's not working or i am not getting results.
Can you please help where might be wrong.
Sub ProtectAllSheets()
Const myPassword = "random"
Dim sh As Worksheet
Dim wBk As Workbook
Dim sFileSpec As String
Dim sPathSpec As String
Dim sFoundFile As String
sPathSpec = Environ("Sheet1") & "C:\Users\vinod.k\Desktop\Test"
sFileSpec = "*.xlsx"
sFoundFile = Dir(sPathSpec & sFileSpec)
Application.DisplayAlerts = False
Do Until sFoundFile = ""
Set wBk = Workbooks.Open(sPathSpec & sFoundFile)
If Not wBk Is Nothing Then
With wBk
For Each sh In .Worksheets
sh.Protect Password:=myPassword, AllowInsertingRows:=False, AllowDeletingRows:=False, AllowSorting:=False, AllowFiltering:=False
Next sh
.Save
Debug.Print "Saved: " & .FullName
.Close
End With
Set wBk = Nothing
End If
sFoundFile = Dir
Loop
Application.DisplayAlerts = True
End Sub
I'm new to this macro. I found following macro to protect sheet in a folder. But somehow it's not working or i am not getting results.
Can you please help where might be wrong.
Sub ProtectAllSheets()
Const myPassword = "random"
Dim sh As Worksheet
Dim wBk As Workbook
Dim sFileSpec As String
Dim sPathSpec As String
Dim sFoundFile As String
sPathSpec = Environ("Sheet1") & "C:\Users\vinod.k\Desktop\Test"
sFileSpec = "*.xlsx"
sFoundFile = Dir(sPathSpec & sFileSpec)
Application.DisplayAlerts = False
Do Until sFoundFile = ""
Set wBk = Workbooks.Open(sPathSpec & sFoundFile)
If Not wBk Is Nothing Then
With wBk
For Each sh In .Worksheets
sh.Protect Password:=myPassword, AllowInsertingRows:=False, AllowDeletingRows:=False, AllowSorting:=False, AllowFiltering:=False
Next sh
.Save
Debug.Print "Saved: " & .FullName
.Close
End With
Set wBk = Nothing
End If
sFoundFile = Dir
Loop
Application.DisplayAlerts = True
End Sub