Renaming Files Loop order - MS Scripting Runtime

DaveBlakeMAAT

Board Regular
Joined
Feb 28, 2016
Messages
182
Hi

I am having a bit of a brain fart on this particular problem when it comes to ordering my loops correctly. I have a folder of scanned invoices (file names are sequentially numbered), I also have a spreadsheet which contains the invoice numbers in column A and a Page Count in Column B (one invoice can have multiple scanned image files.

I am fine with using Scripting Runtime and looping through each file in the nominated folder. Where I am struggling is getting my loops in the correct order!

My objective is to rename the files so that they are prefixed with the invoice number and suffixed with Page "x".

Any pointers in the right direction would be very much appreciated!

Regards

Dave
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,999
Is there anything which relates the invoice numbers in the cells to the existing file names? Give examples of the invoice number and its corresponding file name. It might be easier to loop through the cells and find the corresponding file, instead of looping through the folder.
 

DaveBlakeMAAT

Board Regular
Joined
Feb 28, 2016
Messages
182
Is there anything which relates the invoice numbers in the cells to the existing file names? Give examples of the invoice number and its corresponding file name. It might be easier to loop through the cells and find the corresponding file, instead of looping through the folder.
Hi John

unfortunatly there is no connection between the file names and the spreadsheet. The files are given a serial file name by the scanner, and therefore the only thing I can rely on is that the files will be listed in the same order as the rows in the spreadsheet.
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,999

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,999
UPDATE - the above Name statement renames the files to "[column A cell] Page [column B cell]", losing the original file name.

You talked about a prefix and a suffix - if you want to keep the original file name and add the prefix and suffix, then change the Name statement to:

Code:
        Name path & files(n) As path & ActiveSheet.Cells(n + 2, "A") & Left(files(n), p - 1) & " Page " & ActiveSheet.Cells(n + 2, "B") & Mid(files(n), p)
I suggest testing the code on a copy of your folder to ensure it renames the files correctly.
 

DaveBlakeMAAT

Board Regular
Joined
Feb 28, 2016
Messages
182
Thanks John

Gonna be a bit busy the next few days so won't have the chance to play around with it until the weekend, but will report back once tested.
 

DaveBlakeMAAT

Board Regular
Joined
Feb 28, 2016
Messages
182
Thanks John

I understand your approach, generate a list of required file names, which you are storing in an array, then use that when looping through the files. That approach is going to work perfectly for me, I was overthinking the problem!

Many thanks

Regards

Dave
 

Forum statistics

Threads
1,082,342
Messages
5,364,783
Members
400,815
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top