Run-time error 70 - 'permission denied'

%3CLINGO-SUB%20id%3D%22lingo-sub-2339910%22%20slang%3D%22en-US%22%3ERun-time%20error%2070%20-%20'permission%20denied'%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2339910%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20all%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20you%20are%20doing%20well!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20struggling%20to%20run%20a%20macro%20I%20encountered%20on%20the%20web%20and%20I%20can't%20run%20it%20due%20to%20the%20following%20error%3A%20%22Run-time%20error%2070%20-%20'permission%20denied'%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20CommandButton2()%3CBR%20%2F%3E'Microsoft%20Scripting%20Runtime%20reference%20should%20be%20enabled%3C%2FP%3E%3CP%3EDim%20fso%20As%20New%20FileSystemObject%3CBR%20%2F%3EDim%20fo%20As%20Folder%3CBR%20%2F%3EDim%20f%20As%20File%3CBR%20%2F%3EDim%20last_row%20As%20Integer%3CBR%20%2F%3Elast_row%20%3D%20Worksheets(%22Sheet1%22).Cells(Rows.Count%2C%201).End(xlUp).Row%3CBR%20%2F%3ESet%20fo%20%3D%20fso.GetFolder(Worksheets(%22Sheet1%22).Cells(2%2C%205).Value)%3CBR%20%2F%3EDim%20new_name%20As%20String%3CBR%20%2F%3EFor%20Each%20f%20In%20fo.Files%3CBR%20%2F%3EFor%20i%20%3D%202%20To%20last_row%3CBR%20%2F%3EIf%20f.Name%20%3D%20Worksheets(%22Sheet1%22).Cells(i%2C%201).Value%20Then%3CBR%20%2F%3Enew_name%20%3D%20Worksheets(%22Sheet1%22).Cells(i%2C%202).Value%3CBR%20%2F%3Ef.Name%20%3D%20new_name%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%3CBR%20%2F%3ENext%3CBR%20%2F%3EMsgBox%20(%22Done%22)%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20a%20very%20simplistic%20macro%2C%20column%20A%20(from%20row%202%20downwards)%20contains%20incumbent%20pdf%20names%2C%20column%20B%20(from%20row%202%20downwards)%20comprises%20the%20new%20pdf%20names%20I%20aim%20to%20set.%20My%20directory%20is%20Cell%20E2.%20Prior%20to%20running%20the%20macro%20I%20activated%20the%20Microsoft%20Scripting%20Runtime%20reference.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20be%20grateful%20if%20anyone%20proposes%20a%20solution%20to%20my%20mishap.%20My%20knowledge%20with%20regards%20to%20VBA%2Fprogramming%20is%20relatively%20rudimentary%2C%20so%20it%20would%20be%20great%20if%20the%20solution%20can%20be%20easily%20comprehended.%20My%20operating%20system%20is%20Microsoft%20365%20for%20enterprise%20(my%20workplace%20laptop).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%20for%20your%20collaboration.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20Wishes%2C%3C%2FP%3E%3CP%3EFilip%20Dyankov%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2339910%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

Dear all,

 

Hope you are doing well!

 

I am struggling to run a macro I encountered on the web and I can't run it due to the following error: "Run-time error 70 - 'permission denied'"

 

Sub CommandButton2()
'Microsoft Scripting Runtime reference should be enabled

Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
Dim last_row As Integer
last_row = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Set fo = fso.GetFolder(Worksheets("Sheet1").Cells(2, 5).Value)
Dim new_name As String
For Each f In fo.Files
For i = 2 To last_row
If f.Name = Worksheets("Sheet1").Cells(i, 1).Value Then
new_name = Worksheets("Sheet1").Cells(i, 2).Value
f.Name = new_name
End If
Next
Next
MsgBox ("Done")
End Sub

 

It is a very simplistic macro, column A (from row 2 downwards) contains incumbent pdf names, column B (from row 2 downwards) comprises the new pdf names I aim to set. My directory is Cell E2. Prior to running the macro I activated the Microsoft Scripting Runtime reference.

 

I would be grateful if anyone proposes a solution to my mishap. My knowledge with regards to VBA/programming is relatively rudimentary, so it would be great if the solution can be easily comprehended. My operating system is Microsoft 365 for enterprise (my workplace laptop).

 

Thank you in advance for your collaboration.

 

Best Wishes,

Filip Dyankov

1 Reply

@tigerinsofia 

The macro in your sample workbook is missing a Next (the version in your post does have it).

 

I did the following:

  • I created a folder with a few files.
  • I let the first command button create a list of the files in column A.
  • I entered new names for the files in column B.
  • I clicked the second command button.

It renamed the files without problems.

 

Could the files in your folder be read-only?