Run-time error 70 - 'permission denied'

Copper Contributor

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?