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
Richard, Software Specialist
Category: Microsoft Office
Satisfied Customers: 16842
Experience:  Over 15 year experience resolving Microsoft Office Issues
32989067
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

C:\Jobs\-3432 - XXXXX XXXXX - PEPSI COLA - UPSTREAM PRODUCTIONS

with subfloders:

C:\Jobs\010 - -3432 - CLIENT INFO - PEPSI COLA - XXXXX XXXXX

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

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):

C:\Jobs\8000 - XXXXX XXXXX - PEPSI COLA - UPSTREAM PRODUCTIONS

with subfloders:

C:\Jobs\010 - 8000 - CLIENT INFO - PEPSI COLA - XXXXX XXXXX

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

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

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

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: 1 year ago.
Category: Microsoft Office
Expert:  Richard replied 1 year ago.
Thank you for your question, my name is Richard.

Can I look at the file please?

- Click the following link: http://www.wikisend.com
- 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago.

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


Files:


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?


 


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 1 year 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()
Loop

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 1 year 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 1 year ago.
ok, I have the Folder Loop for you

I put it in Excel as it may get malformed here

Go to www.wikisend.com

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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago.

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


I have directed the line:


FoldersArray = funcGetSubfolders("C:\testt")


and


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


to a folder by that name and path,


and I have directed it to:


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


and


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


Both folders exist with sub folders and files

Expert:  Richard replied 1 year ago.
You put a \ at the end

no \ at the end please for the path

Customer: replied 1 year 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.


 


wikisend



  • File ID: 246150

Expert:  Richard replied 1 year ago.
Change

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

To

Name FoldersArray(i) As FoldersArray(i)

Does it then work?

Customer: replied 1 year 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 1 year ago.
This part please as this is the renaming part

Name FoldersArray(i) As FoldersArray(i)

Customer: replied 1 year 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

 

Richard,

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

Expert:  Richard replied 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago.

Hello,

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,

Rachel

Customer: replied 1 year 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.


 


Lance

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • jstinehelfer

    Information Systems Manager

    Satisfied Customers:

    36
    A+ Comptia Certified computer repair
< Last | Next >
  • http://ww2.justanswer.com/uploads/JS/jstinehelfer/2012-6-6_215146_Pictureofme1.64x64.png jstinehelfer's Avatar

    jstinehelfer

    Information Systems Manager

    Satisfied Customers:

    36
    A+ Comptia Certified computer repair
  • http://ww2.justanswer.com/uploads/JA/JasonJames122/2011-12-16_135647_jasonjamesheadshotweb.64x64.jpg JasonJames122's Avatar

    JasonJames122

    Computer Enthusiast

    Satisfied Customers:

    0
    I manage all information systems (network of Windows PC's and servers with Microsoft Office products) for a small business
  • http://ww2.justanswer.com/uploads/JE/jessmagz/2012-6-6_18129_jm.64x64.jpg Jess M.'s Avatar

    Jess M.

    Computer Support Specialist

    Satisfied Customers:

    301
    Computer Software Specialist for more than 10 years
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Computer Manager

    Satisfied Customers:

    274
    20 years experience providing remote computer support
  • http://ww2.justanswer.com/uploads/SP/spherrod/2012-6-6_174244_1000852.64x64.JPG Steve Herrod's Avatar

    Steve Herrod

    Computer Support Specialist

    Satisfied Customers:

    125
    Trained in Microsoft Office from 2000 through to latest version 2010.
  • http://ww2.justanswer.com/uploads/SE/Seashore2011/2011-12-6_33418_111205223015.64x64.jpg Chris L.'s Avatar

    Chris L.

    Support Specialist

    Satisfied Customers:

    124
    Certified Expert with over 10 years experience.
  • http://ww2.justanswer.com/uploads/KR/krkljatorm/2012-6-24_153417_ITMiro.64x64.jpg IT Miro's Avatar

    IT Miro

    Computer Scientist

    Satisfied Customers:

    97
    Bachelor's Degree in Information Technology, Microsoft Certified Professional
 
 
 

Related Microsoft Office Questions

Chat Now With A Microsoft Office Technician
Richard
Richard
Microsoft Office Technician
16842 Satisfied Customers
Over 15 year experience resolving Microsoft Office Issues