Macro Help

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
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
It is possible that the line of code needs a back slash

sPathSpec = Environ("Sheet1") & "C:\Users\vinod.k\Desktop\Test"

Change to

sPathSpec = Environ("Sheet1") & "C:\Users\vinod.k\Desktop\Test\"
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,925
Members
449,056
Latest member
denissimo

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