Find last filename and add +1

robwels

Board Regular
Joined
May 21, 2002
Messages
74
Hi all.
I have the following question.
Have invoices with filename 2003001 etc. What i would like to do is automaticly open the blanc invoice file, rename it to last filename +1,
have that value in cell c3

Can this be done in a macro or vba, and can anybody help me with the code?

Thanks.

Rob
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
the easiest way is to store the file name in the blank file when you save it (say in cell A65536) - eg

last file was 10.xls

you would have

Cells(65536,1) = Cells(65536,1) + 1

ActiveWorkbook.Save

ActiveWorkbook.SaveAs ("C:\....\"&Cells(65536,1) & ".xls")

etc...

obviously you would need to clear fields etc to keep the master file as blank.

You could return the directory contents (ie. all file names), but if the file names are a mix of text and numeric values then this will be a little cumbersome - if purely numeric then you can run a simple max function on the list and add one to that value. The choice as Cilla would say, is yours.
 
Upvote 0
Thanks for the reply.
Is it also possible by filename when i have for instance
the filenames 2003001.xls, 2003002.xls in the same directory and i want the new invoice to be the last one +1?

Regards,

Rob
 
Upvote 0
sure - even though it's a combo of year and number adding one will not affect the year ref etc...

Are you saying that you want to do this by extracting the directory list and adding one to the last value? If so let me know.
 
Upvote 0
Yes exactly. I have a blanc invoice called invoiceblanc.xls. When this in opened the user wants to make a new invoice, excel must take the last filename +1, rename the filename (2003003+1), put this number in cell c3 as invoicenumber. The user only have to save the file and the blanc invoice stays the same.

Thanks again.

Rob
 
Upvote 0
Rob, couple of things that need to be established:

1. When the blank file is saved by a user as a new file is it stored in a network directory or on their own hard drive? i.e. regardless of user are the files all stored in the same directory to which all users have access?

2. Is the blank file kept in a network location as read-only or does every use store a version of this file on their own PC?
 
Upvote 0
OK say let's assume a couple of things for the code below:

1. Blank file is called MASTER.xls
2. A blank sheet called Sheet1 exists in the MASTER file
3. The new files are saved to the following directory C:\MrE\...
4. The main sheet in the file is called MAIN

Sub RETURN_FILES()

Application.ScreenUpdating = True

Sheets("Sheet1").Select

f = Dir("C:\MrE\*.xls")
n = 1

Do While Len(f) > 0
Sheets("SHEET1").Cells(n, 1) = Left(f, Len(f) - 4)
n = n + 1
f = Dir()
Loop

Columns("A:A").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

Cells(1, 1).Select
Flename = (Cells.End(xlDown).Value) + 1

ActiveWorkbook.SaveAs ("C:\MrE\" & Flename & ".xls")

Sheets("MAIN").Select

Application.ScreenUpdating = False

End Sub


Hope that helps
 
Upvote 0
Thanks, i try this.
Could you pls. explain the code to me?
I would like to understand what i am doing.

Greetings,

Rob
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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