Hi guys,
Was hoping if someone could help me with this code, it seems to run fun, but there are times i get the following error:
Run-time error '53' file not found
The file is in there, but i'm not sure if the file name for e.g Tota - Pay Hours - Overtime - 22.xls is causing the error due to the "-"
Can someone please assist if there code below needs to be changed as it could be causing some form of confusion?
thanks
Was hoping if someone could help me with this code, it seems to run fun, but there are times i get the following error:
Run-time error '53' file not found
The file is in there, but i'm not sure if the file name for e.g Tota - Pay Hours - Overtime - 22.xls is causing the error due to the "-"
Can someone please assist if there code below needs to be changed as it could be causing some form of confusion?
thanks
VBA Code:
Sub RenameFiles2()
Dim myPath As String: myPath = Range("B3")
Dim r As Long: r = 8
Dim i As Long
Dim fn As String, ext As String, fn2 As String
Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 2))
fn = Cells(r, 2).Value
If Dir(myPath & fn) <> "" Then
ext = Right(fn, InStrRev(fn, ".") - 1)
fn2 = myPath & Left(fn, Len(fn) - InStrRev(fn, "."))
i = 1
Do Until Dir(fn2 & " - " & i & "." & ext) = ""
i = i + 1
Loop
fn = fn2 & " - " & i & "." & ext
End If
Name myPath & Cells(r, 1).Value As myPath & fn
r = r + 1
Loop
End Sub