VBA Open File & Rename

bortolini

New Member
Joined
Sep 14, 2016
Messages
1
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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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