Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Rename a folder to a new name generated from a cell

0

I have a folder called "Current" which contains a number of pdf files. I want to change it to a new name, but in the same directory, and with the new name being generated from a cell within a worksheet. I have looked at various tutorials and solutions online, but none of them do this particular task. I am a novice without any IT background, but have made a number of complex macros in excel in the past to automate various functions in my day to day work in my office job.

Example; I can use the following Macro which works, but as I will be recreating the folder "Current" and renaming it over and over again using a name generated from within the worksheet, I need the "CurrentRename" part of this macro to be the variable name.

Sub RenameFolder()

    Dim oldFolderName As String

    Dim newFolderName As String

    ' Define the full paths for the old and new folders

    oldFolderName = "C:\Users\allia\Documents\AW\PDF\Current"

    newFolderName = "C:\Users\allia\Documents\AW\PDF\CurrentRename"

    ' Check if the old folder exists to prevent an error

    If Len(Dir(oldFolderName, vbDirectory)) > 0 Then

        ' Rename the folder

        Name oldFolderName As newFolderName

        MsgBox "Folder renamed successfully!", vbInformation

    Else

        MsgBox "Error: The original folder was not found.", vbCritical

    End If

End Sub

Any help would be greatly appreciated.

Answer
Discuss

Discussion

Hi Alliams and welcome to the Forum.

Should be fairly easy to do that but will your Excel file (with the new macro) be stored in a folder other than "Current"? 

Also does your cell (whose text  will ve used to rename "Current") have any data valuation to exclude illegal characters in the folder name)?
John_Ru (rep: 6762) Oct 28, '25 at 10:22 am
Hi John

The new folder will be in the same main folder  as "Current" (Current is a subfolder of "PDF", as will the new renamed folder be).

As I said, I am a novice, so I am unsure about the second part of your question. The filename will just be a 4 digit number. I hope that answers part 2 of your question. Thank you for your response.

Kind regards

Alan
alliams (rep: 2) Oct 28, '25 at 12:03 pm
Thanks for the reply. Alan. Looks like  Willie has provided an Answer before I even saw your reply! Hopefully that sorts it for you. 

I asked about data validation since there are certain characters which are not allowed in file (or path) names under Windows. That won't be an issue if the folder name is just numbers.
John_Ru (rep: 6762) Oct 28, '25 at 1:48 pm
Add to Discussion

Answers

0
Selected Answer

Hello alliams,

As John has said, this is fairly easy.

First you need to declare "CurrentRename" as a variable.

Dim CurrentRename As String

Second you then need to give this variable a value; such as: 

CurrentRename = Sheet1.Range("H6").Value

Change the cell reference to suit your needs.

Third, change this line: 

newFolderName = "C:\Users\allia\Documents\AW\PDF\CurrentRename"

to: 

newFolderName = "C:\Users\allia\Documents\AW\PDF\" & CurrentRename

Also, as recommended by John, you might want to add some data validation to the cell to prevent illegal characters in the file name.

If this solves things for you please mark my answer as Slected.

Cheers   :-)

Discuss

Discussion

Hi WilliD24!!! Thank you ever so much. It has worked like a dream. I am so grateful!!! Would never have been able to do this without your help.
alliams (rep: 2) Oct 28, '25 at 7:39 pm
Glad I was able to help and Thanks for selecting my answer.

Cheers   :-)
WillieD24 (rep: 707) Oct 28, '25 at 8:28 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login