Renaming a file in VBA

donkey shrek

New Member
Joined
Nov 15, 2022
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi all, I am new to VBA & I'm a little confused with this code.

I am trying to rename a file so that it will be uploaded to a shared drive.

Variable oldname has only been used at this instance, which is to assign the old path to it. My main question here is that it's hence, redundant right?
When trying to rename a file, we don't need to reference it by calling out the full file path, we can just use ActiveWorkbook?

VBA Code:
oldname = ThisWorkbook.Path & "\CRMS.xlsx"
newname = sharepointpath & "\" & filename & ".xlsx"
ActiveWorkbook.SaveAs newname
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi,

and where in the sniplet do sharepointpath as well as filename get their values? I would choose a different name for the variable than filename which is part of the Parameters for SaveAs.

VBA Code:
Sub MrE1222174_RenameFileDifferentFolder()
'will move the file
oldname = ThisWorkbook.Path & "\CRMS.xlsx"
newname = sharepointpath & "\CRMS.xlsx"
Name oldname As newname
End Sub
VBA Code:
Sub MrE1222174_CreateCopyDifferentFolder()
'will create a copy of workbook at new place
Dim strNewWB As String
'sample path is Desktop
strNewWB = Environ("UserProfile") & "\CRMS.xlsx"
ActiveWorkbook.SaveAs strNewWB, FileFormat:=51
End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,223,637
Messages
6,173,489
Members
452,515
Latest member
archcalx

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