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!

Path and Filenames that had been selected to RenameInput New Filenames Below
P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test1_jun.xlsmP:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test1_jul.xlsm
P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test2_jun.xlsmP:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test2_jul.xlsm
P:\Gen-Acct\OilAcctg\OILDEPT\FY18\Jun\test3_jun.xlsmP:\Gen-Acct\OilAcctg\OILDEPT\FY18\jul\test3_jul.xlsm

<colgroup><col><col></colgroup><tbody>
</tbody>


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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,215,425
Messages
6,124,826
Members
449,190
Latest member
rscraig11

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