Find last filename and add +1 :: MrExcel Message Board



 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Find last filename and add +1
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

robwels
Board Regular


Joined: 22 May 2002
Posts: 74


Status: Offline

 Reply with quote  

Find last filename and add +1

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

Post Mon Feb 24, 2003 9:59 am 
 View user's profile Send private message

lasw10
Board Master
Board  Master


Joined: 07 Sep 2002
Posts: 1936
Location: TCR, London, UK
Flag: England

Status: Offline

 Reply with quote  

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.

Post Mon Feb 24, 2003 10:06 am 
 View user's profile Send private message Send e-mail Visit poster's website

robwels
Board Regular


Joined: 22 May 2002
Posts: 74


Status: Offline

 Reply with quote  

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

Post Mon Feb 24, 2003 10:25 am 
 View user's profile Send private message

lasw10
Board Master
Board  Master


Joined: 07 Sep 2002
Posts: 1936
Location: TCR, London, UK
Flag: England

Status: Offline

 Reply with quote  

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.

Post Mon Feb 24, 2003 10:29 am 
 View user's profile Send private message Send e-mail Visit poster's website

robwels
Board Regular


Joined: 22 May 2002
Posts: 74


Status: Offline

 Reply with quote  

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

Post Mon Feb 24, 2003 10:56 am 
 View user's profile Send private message

lasw10
Board Master
Board  Master


Joined: 07 Sep 2002
Posts: 1936
Location: TCR, London, UK
Flag: England

Status: Offline

 Reply with quote  

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?

Post Mon Feb 24, 2003 11:01 am 
 View user's profile Send private message Send e-mail Visit poster's website

robwels
Board Regular


Joined: 22 May 2002
Posts: 74


Status: Offline

 Reply with quote  

Is a stand alone computer with read only file.

Rob

Post Mon Feb 24, 2003 11:46 am 
 View user's profile Send private message

lasw10
Board Master
Board  Master


Joined: 07 Sep 2002
Posts: 1936
Location: TCR, London, UK
Flag: England

Status: Offline

 Reply with quote  

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

Post Mon Feb 24, 2003 12:04 pm 
 View user's profile Send private message Send e-mail Visit poster's website

robwels
Board Regular


Joined: 22 May 2002
Posts: 74


Status: Offline

 Reply with quote  

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

Greetings,

Rob

Post Mon Feb 24, 2003 12:59 pm 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.