Macros to Grab File Name, List and Rename in Folder Getting 'File not found' Error

jbenfrancis1

New Member
Joined
Feb 22, 2022
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have these two macros working together to grab the file names in a folder and insert them onto sheet "File Names" in column A. I then have a list of Names I want them to be changed to in Column B. However when I run the renamefiles macro I receive File Not Found errror. No files names are duplicates.

VBA Code:
Sub getfile()

Dim myPath As String
Dim myFile As String

myPath = "C:\Users\joe\Desktop\2.23"
myFile = Dir(myPath & "\*.csv")

r = 1
Do While myFile <> ""
   Worksheets("File Names").Cells(r, 1).Value = myFile
   r = r + 1
   myFile = Dir

Loop

End Sub

Code:
Sub RenameFile()

Dim myPath As String
myPath = "C:\Users\joe\Desktop\2.23"

Dim ws As Worksheet
Set ws = Worksheets("File Names")
r = 1

Do Until IsEmpty(ws.Cells(r, 1)) And IsEmpty(ws.Cells(r, 2))

   Name myPath & ws.Cells(r, 1).Value As myPath & ws.Cells(r, 2).Value
    
    
    r = r + 1
Loop

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You forgot the backslash after "myPath" in your second procedure.

You accounted for it here in your first procedure:
Rich (BB code):
myFile = Dir(myPath & "\*.csv")
but it looks like you didn't in your second procdure.

Note: A quick easy way to see what it is trying to do is to temporary replace your rename step with a message box tht returns the name you are trying to copy from and to, i.e.
temporarily replace this line:
VBA Code:
   Name myPath & ws.Cells(r, 1).Value As myPath & ws.Cells(r, 2).Value
with this:
VBA Code:
    MsgBox myPath & ws.Cells(r, 1).Value & vbCrLf & myPath & ws.Cells(r, 2).Value
and run it to see what file names it is trying to use.
 
Last edited:
Upvote 0
You forgot the backslash after "myPath" in your second procedure.

You accounted for it here in your first procedure:
Rich (BB code):
myFile = Dir(myPath & "\*.csv")
but it looks like you didn't in your second procdure.

Note: A quick easy way to see what it is trying to do is to temporary replace your rename step with a message box tht returns the name you are trying to copy from and to, i.e.
temporarily replace this line:
VBA Code:
   Name myPath & ws.Cells(r, 1).Value As myPath & ws.Cells(r, 2).Value
with this:
VBA Code:
    MsgBox myPath & ws.Cells(r, 1).Value & vbCrLf & myPath & ws.Cells(r, 2).Value
and run it to see what file names it is trying to use.
Im not exactly following where the back slash would need to go after mypath in procedure 2
 
Upvote 0
Im not exactly following where the back slash would need to go after mypath in procedure 2
Did you try my suggestion regarding the message box?
This will show you exactly what the two file path & names you are building.
Once you see that, I think the issue will become obvious to you.

This is also a good debugging technique to learn. When something isn't working the way you expect, use Message Boxes to see exactly what the value is that you are building.
 
Upvote 0
You are welcome.

The quickest/easiest fix is to just place a backslash at the end of your path, i.e.
Rich (BB code):
Sub RenameFile()

Dim myPath As String
myPath = "C:\Users\joe\Desktop\2.23\"

Dim ws As Worksheet
Set ws = Worksheets("File Names")
r = 1

Do Until IsEmpty(ws.Cells(r, 1)) And IsEmpty(ws.Cells(r, 2))

   Name myPath & ws.Cells(r, 1).Value As myPath & ws.Cells(r, 2).Value
    
    
    r = r + 1
Loop

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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