I am having trouble getting the correct "On Error" statement in my VBA code. I use a macro to list the file path in column A and copy and paste into column B and rename the files. If i list a file in column A that doesn't exist i want the code to continue onto the next file name in column A. As of now, it will rename the next file in column A as the prior row in column B. I.E. test 2 doesn't exist so it renames test 3 as test 2.
the only macro i could find on this just renames the files without opening them which will screw up my links. i used a combination of that code along with code that opens a file, performs a macro, and continues onto the next file in a folder... any help is appreciated!
[TABLE="width: 555"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Path and Filenames that had been selected to Rename[/TD]
[TD]Input New Filenames Below[/TD]
[/TR]
[TR]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test1_jun.xlsm[/TD]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test1_jul.xlsm[/TD]
[/TR]
[TR]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test2_jun.xlsm[/TD]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test2_jul.xlsm[/TD]
[/TR]
[TR]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test3_jun.xlsm[/TD]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test3_jul.xlsm[/TD]
[/TR]
</tbody>[/TABLE]
Code Below:
Sub RenameFiles()
Dim z As String
Dim s As String
Dim V As Integer
Dim TotalRow As Integer
TotalRow = ActiveSheet.UsedRange.Rows.Count
For V = 1 To TotalRow
' Get value of each row in columns 1 start at row 2
z = Cells(V + 1, 1).Value
' Get value of each row in columns 2 start at row 2
s = Cells(V + 1, 2).Value
Workbooks.Open Filename:=z, UpdateLinks:=All
ActiveWorkbook.SaveAs Filename:=s, CreateBackup:=False
ActiveWindow.Close
Dim sOldPathName As String
sOldPathName = z
On Error Resume Next
Name sOldPathName As s
Next V
MsgBox "Congratulations! You have successfully renamed all the files"
End Sub
the only macro i could find on this just renames the files without opening them which will screw up my links. i used a combination of that code along with code that opens a file, performs a macro, and continues onto the next file in a folder... any help is appreciated!
[TABLE="width: 555"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Path and Filenames that had been selected to Rename[/TD]
[TD]Input New Filenames Below[/TD]
[/TR]
[TR]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test1_jun.xlsm[/TD]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test1_jul.xlsm[/TD]
[/TR]
[TR]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test2_jun.xlsm[/TD]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test2_jul.xlsm[/TD]
[/TR]
[TR]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test3_jun.xlsm[/TD]
[TD]P:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test3_jul.xlsm[/TD]
[/TR]
</tbody>[/TABLE]
Code Below:
Sub RenameFiles()
Dim z As String
Dim s As String
Dim V As Integer
Dim TotalRow As Integer
TotalRow = ActiveSheet.UsedRange.Rows.Count
For V = 1 To TotalRow
' Get value of each row in columns 1 start at row 2
z = Cells(V + 1, 1).Value
' Get value of each row in columns 2 start at row 2
s = Cells(V + 1, 2).Value
Workbooks.Open Filename:=z, UpdateLinks:=All
ActiveWorkbook.SaveAs Filename:=s, CreateBackup:=False
ActiveWindow.Close
Dim sOldPathName As String
sOldPathName = z
On Error Resume Next
Name sOldPathName As s
Next V
MsgBox "Congratulations! You have successfully renamed all the files"
End Sub