How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Richard Your Own Question
Richard, Software Specialist
Category: Microsoft Office
Satisfied Customers: 36119
Experience:  Over 15 year experience resolving Microsoft Office Issues
Type Your Microsoft Office Question Here...
Richard is online now
A new question is answered every 9 seconds

Excel Macro script simple replace folder and files

Customer Question

Software: Excel 2010
OS: Windows 2007 Home Premium 64bit
Intel i7 CPU w/ 9GB RAM

Looking for a coded macro solution.

Problem:  Our company uses a simple Excel spreadsheet to enter in new basic info for new Estimates and Jobs (Job Number, Client Name, Job Name and Production Company)

Estimate folders and sub-folders are created automatically. The names of each begins with the EstimateNumber.

When an estimate turns into a job, I would like a macro to go and replace the estimate number at the beginning of that specific folder and it's sub-folders with the new job number and also add the job number to each file that has been created in the folder and sub-folders.

I imagine this would be accomplished by by SELECTING the estimate number on a spread sheet and clicking a "Convert to Job button" (which would also change the selected cell to the value in the NextJobNum.txt file - this last part is already scripted.

More Detail: Our Estimate folders are created like this (always with a decreasing negative number, assuming an Estimate Number of -3432)

C:\Jobs\Est No. - Client - Product - Who Pays the bills


with subfloders:


C:\Jobs\010 - -3432 - CLIENT INFO - PEPSI COLA - XXXXX XXXXX\-3432 PRODUCTION NOTES\Schedule.txt

C:\Jobs\010 - -3432 - CLIENT INFO - PEPSI COLA - XXXXX XXXXX\-3432 PRODUCTION NOTES\Job Description.txt


C:\Jobs\020 - -3432 - RENDERINGS -PEPSI COLA - XXXXX XXXXX\-3432 RENDER FILES-A\Client renderings.pdf

C:\Jobs\020 - -3432 - RENDERINGS -PEPSI COLA - XXXXX XXXXX\-3432 RENDER FILES-B\millions of pics.jpg

C:\Jobs\030 - -3432 - SHOP DRAW - PEPSI COLA - XXXXX XXXXX

and so on...

Each folder holds various files of job information We would like them to read when finished (assuming the NextJobNumber located in NextJobNum.txt is 8000):


with subfloders:


C:\Jobs\010 - 8000 - CLIENT INFO - PEPSI COLA - XXXXX XXXXX\8000 PRODUCTION NOTES\8000 - Schedule.txt

C:\Jobs\010 - 8000 - CLIENT INFO - PEPSI COLA - XXXXX XXXXX\8000 PRODUCTION NOTES\8000 - Job Description.txt


C:\Jobs\020 - 8000 - RENDERINGS -PEPSI COLA - XXXXX XXXXX\8000 RENDER FILES-A\8000 - Client renderings.pdf

C:\Jobs\020 - 8000 - RENDERINGS -PEPSI COLA - XXXXX XXXXX\8000 RENDER FILES-A\8000 - million of pics.jpg


It is possible someone may have a file open when this macro is run - perhaps a warning with a delayed renaming try at 1AM?

Or a warning on who has what file open and we can tell them to shut it?

Thanks. Questions? I can send my current code that does everything I need except what is described above.

Submitted: 3 years ago.
Category: Microsoft Office
Expert:  Richard replied 3 years ago.
Thank you for your question, my name is Richard.

Can I look at the file please?

- Click the following link:
- Upload the file to that website
- Once it is uploaded, the resulting page will display a "File ID" number.
- Please, give me that "File ID Number"

Thank you
Customer: replied 3 years ago.

File ID: 439264


In the spreadsheet the user will enter 3 pieces of information in cells E4, E5 and E6.

Then they press a button, either New Job or New Estimate.

At this point the next job or estimate number is XXXXX and the information is copied down to the row below, allowing the next entry.

If the user selects one or more CLIENTS and presses the "Make Folders" button, folders and subfolders are automatically created with their corresponding job or estimate number. At this point the entire office will begin to fill the folders with information regarding the ESTIMATE.

Here's where you come in, if the job is awarded, the ESTIMATE files and folders need to have the ESTIMATE number removed and replaced with the job number.

It all sounds complicated but it's pretty straight forward.

Expert:  Richard replied 3 years ago.
Thanks Lance.

And it is the Convert to Job button they click correct?

I would then look at what row is selected and use the number in Column B and as the Job Number?

Customer: replied 3 years ago.

Kind of.

We would look at the row selected and use the number in column D as the ESTIMATE number then read the number in InvNum.txt as the replacement number - then the number in InvNum.txt should be increased by 1 and saved. (Sub WriteInvoiceNumber() does this BUT is hard coaded to overwrite cell (4,4) or D4 when, for this case it should be the selected row and column #4. In addition, WriteInvoiceNumber also calls Sub InvNumAvailable which has a line or two that will copy row 4 and move it down one row - we don't want these lines for what we are about to do.


Then we can start the renaming of folders and sub folders beginning with the Estimate number. I have started this code as sub FinalConvert() where I have selected the Estimate Number (current row, column #4) but have not changed it to the number in InvNum.txt (increase the text file by one and re-save it)

If you look at the Sub FinalConvert() file, where it says

======== WORKING HERER ==========

I have tried my very first attempt and changing a folder name

Also, we will have to look at the files created under the same folders and simply add the InvNum (which is synonymous with JobNo) beginning of each file name.


I appreciate your help.

If you could be conscious of error handling as you look through my entire code, perhaps we could work something out to write this code as well.


Expert:  Richard replied 3 years ago.
ok, it is very hard to follow what you described, but may not be necessary.

You need to use the Name function to rename the file(s).

So Name "c:\1234Excel.xlsx" As "c:\5678Excel.xlsx"

So if you know the name of the file, can you not rename it using this code?

Using the path and file name you wish?

Name is XXXXX XXXXX to change folder names as well eg:

name c:\folder as c:\Folder1234

Customer: replied 3 years ago.

Yes - "c:\1234Excel.xlsx" As "c:\5678Excel.xlsx" is in my first attempt under the Sub FinalConvert()

Can I call a sub while in the middle of a sub? Like gosub and return?


Regarding "c:\1234Excel.xlsx" As "c:\5678Excel.xlsx"

Assuming we have a specific Folder with multiple branching sub-folders, what might the code look like to append the InvNum to the beginning of each?


Thanks Richie

Expert:  Richard replied 3 years ago.
Yes, no problem, just use

call sub

replace Sub with the name of the Sub (dont use the word Sub, just the name of it)

The code would look like (lest say the InvNum is in variable iNum)

Name "c:\Folder" as "c:\Folder" & iNum

And this will add the Invoice Number to the end of it.

Let me know how you go please

Customer: replied 3 years ago.

Regarding "c:\1234Excel.xlsx" As "c:\5678Excel.xlsx"


I can replace an individual file or folder no problem but I'm looking for help automating the process of all folders and files without knowing what they are called.

Assuming we have a specific Folder with multiple branching sub-folders, what might the code look like to append the InvNum to the beginning of each? How can I append to all *.* the files with the least amount of code?

I won't know what the files are called.


The same is true with the folders - I will know the names of the folders created automatically, but the users will create folders as they require.


Expert:  Richard replied 3 years ago.
We cannot rename something Lance, if we do not know what its called.

Automated or Manual this is not possible. We need to know the name of the file or folder in order to rename it.

Else how do we know what to rename?
Customer: replied 3 years ago.

Exactly - that's part of the help I'm looking for.


How could we append a string in InvNum to the beginning of any and all files (*.*) under a know folder that extends to all of it's sub folders?



What code might append InvNum to the beginning of ALL folders and sub folders under a known directory?


I'm not going to look up every file and folder and perform a single command to each, but the script should. Does that make sense?

Expert:  Richard replied 3 years ago.
ok, then you can do it this way

Dim strFolder As String
Dim strFile As String

strFolder = "C:\SomeFolder\"

strFile = Dir(strFolder & "\*.*")

Do While Len(strFile) > 0

Name strFolder & strFile As strFolder & replace(strFile,".xlsx","") & invNum & ".xlsx"
strFile = Dir()

That will do all the files

For folders how will you know which Folder or Drive to start on?

Or you know this?

Customer: replied 3 years ago.

That looks closer to what I was looking for. Let me work with that for a moment.


Regarding the start folder, I'm developing this code for the first folder:

I'm trying to make sure the directory exists with the EstNo and IF it does then go ahead and start changing all the folder names.


If Dir(Root & "\" & Client & "\" & EstNo & " - " & Product & " - " & Client & " - " & ProdCo & "\") = "" Then
MkDir Root & "\" & Client & "\" & JobNo & " - " & Product & " - " & Client & " - " & ProdCo & "\"
End If


Where Root is hard-coded as a variable

And the following I'll need to revise to be selected row along with the specific Columns

JobNo = rng(r, c - 1)
Client = rng(r, c + 10)
Product = rng(r, c + 11)
ProdCo = rng(r, c + 12)



Expert:  Richard replied 3 years ago.
ok, I have the Folder Loop for you

I put it in Excel as it may get malformed here

Go to

File ID 339824

You will see the first Sub, you can set how you wish to rename the folder as you wish.

You also set the starting folders.

Keep my two other functions below it as these are need.

You can copy it all to your own code.

Let me know if you have any difficulties please

Thank you
Customer: replied 3 years ago.

Thanks Richie -

I'm trying to run your code on it's own but I must be missing something?

according to your code:

FoldersArray = funcGetSubfolders("C:\testt")

I've created a folder C:\testt with a number of subfolders and files.

Can I run this on it's own? I get a "Path/File access error". I have none of the folders or files open.


In your first few lines you talk about a new document, do you mean a document in memory or a text document? Need I do or provide something here?

Expert:  Richard replied 3 years ago.
Replace c:\testt with your own path to the top folder.

No, ignore those comments, there for another project I did.

But Path/File access error means c:\testt foes not exist or you have not the permissions.

But just put your own path in to the folder Lance.

Customer: replied 3 years ago.

I can't get past the "Path/File access error"

I can clearly access the path I'm entering and I can create and change files via my macros.


Could there be a module that you're using that I must grant access to or authorize?


I really am trying hard to see your code work. I've closed all my applications and opened only your script. Tried various directories. Same error. I try my script for creating directories and it works fine.


Thanks Richie -


Expert:  Richard replied 3 years ago.
Run it within the file I gave you.

Does it run there? I test many times and it runs here, so first, before you copy the code to another file, run it within the file I gave please (that you downloaded)

Let me know.

Customer: replied 3 years ago.

Yes. Correct. I have downloaded your file twice and run only that file.

I have directed the line:

FoldersArray = funcGetSubfolders("C:\testt")


FoldersArray = funcGetSubfolders("C:\testt\")

to a folder by that name and path,

and I have directed it to:

FoldersArray = funcGetSubfolders("C:\Jobs\Brendan McDear")


FoldersArray = funcGetSubfolders("C:\Jobs\Brendan McDear\")

Both folders exist with sub folders and files

Expert:  Richard replied 3 years ago.
You put a \ at the end

no \ at the end please for the path

Customer: replied 3 years ago.

Here's a screen grab of the error and the directory that exists.

Yes, I tried it with and without a "\" at the end. No luck either way.

I'll keep the "\" off the end in the future.



  • File ID: 246150

Expert:  Richard replied 3 years ago.

Name FoldersArray(i) As FoldersArray(i) & "ADD WHAT YOU WISH"


Name FoldersArray(i) As FoldersArray(i)

Does it then work?

Customer: replied 3 years ago.

Richie - that solves the error.

Now it runs with no errors but does not seem to do anything.
No changes to files or folders under C:\testt

Which part should I look at to append (or is it pre-pend?) to the beginning of the Folder names?




Expert:  Richard replied 3 years ago.
This part please as this is the renaming part

Name FoldersArray(i) As FoldersArray(i)

Customer: replied 3 years ago.

I can't seem to see where I'll add my JobNum to append to the beginning of the folders - for that matter I can't append anywhere.


I've tried the following:


Name FoldersArray(i) As JobNum & FoldersArray(i)


Name FoldersArray(i) As FoldersArray(i) & "2020"

Gives the Access Error


Name FoldersArray(i) As "2020" & FoldersArray(i)

Invalid procedure call in argument


Name FoldersArray(i) As FoldersArray & "2020" & (i)

Type mismatch error



What happens when you run the exact script you gave me?

Expert:  Richard replied 3 years ago.
ok, I see your problem

First, you cannot do it before (as that is c:\)

you need to use this code, just replace Sub Demo with this

Sub Demo()

Dim FoldersArray As Variant
Dim i As Integer
Dim sfoldername As String

'Read all subfolders of the specified folder into an array
'by calling the funcGetSubfolders function
FoldersArray = funcGetSubfolders("C:\testt")

'Put the results (the array values) into the current document if it is blank,
'or else into a new document
For i = LBound(FoldersArray) To UBound(FoldersArray)
sfoldername = FoldersArray(i)
Name sfoldername As Left(sfoldername, Len(sfoldername) - 1) & "h"
Next i

End Sub

Replace "h" with what you want please

Let me know if you have any difficulties please

Customer: replied 3 years ago.

did you run this? what does it do on your computer?


On mine it replaces what ever folder I type into the:

FoldersArray = funcGetSubfolders("C:\test")


so C:\test becomes C:\testh but no sub folders are touched.

and then a "file not found error"


A couple of things, I need the 'h' to go at the beginning of the folder name.


I fell we are getting closer.

Expert:  Richard replied 3 years ago.
yes I did

but I see the problem for you, when you rename

eg: c:\test to c:\testh

Then it fails the next time because c:\test does not exist anymore (it is c:\testh)

Thats also why you get the error second time you run it as you do not update code to be c:\testh

So doing it at the start will never work as you cannot keep the path.

Doing it at the end also has the same issue.

I think to think this one through, as it is 1am here.

Can we resume tomorrow?
Customer: replied 3 years ago.

Of course.

I'll be at work all day, but will check in in the evening.


For me, it would only change the root folder, then error. I could run a second time but it would still error.


I could change the folder back to "test" and run again, it changes "test" to testh" then errors.


I think if it ends up working for you, it should work for me.


I'll upload my test folders (they are very simple):


Wikisend: File ID: 127600

Expert:  Richard replied 3 years ago.
I looked at this for a number of hours Lance, but the last part of this question, renaming the sub folders, I cannot see how you can do it,.

The problem is you are changing the path, so the next time it loops it has an issue.

I even looked at doing it via a batch file but cannot see how.

Sorry but Ill need to opt out for another expert that may have other ideas.

Expert:  Rachel-Mod replied 3 years ago.


I’m Rachel, and I’m moderator for this topic. It seems the professional has left this conversation. This happens occasionally, and it's usually because the professional thinks that someone else might be a better match for your question. I've been working hard to find a new professional to assist you right away, but sometimes finding the right professional can take a little longer than expected.

I was checking to see if you had already found your answer or if you still needing assistance from another one of the professionals?

Please let me know if you wish to continue waiting or if you would like for us to close your question.

Also remember that JustAnswer has a multitude of categories to help you with all your needs from Health, Pets, Computers, Taxes, Cars, Finance, Law, to Home Improvement, and more.

Thank you,


Customer: replied 3 years ago.

Rachel - I'd like to close the question at this time. If I can't find the answer by next weekend, I'll try again later.


Thanks for the attempt.