Find last filename and add +1

Thanks Thanks:  0
Likes Likes:  0
Results 1 to 9 of 9

Thread: Find last filename and add +1

  1. #1
    Board Regular
    Join Date
    May 2002
    Posts
    74
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default 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

  2. #2
    MrExcel MVP DonkeyOte's Avatar
    Join Date
    Sep 2002
    Location
    Suffolk, UK
    Posts
    9,118
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.
    Does my a$$ look big in this picture ?

  3. #3
    Board Regular
    Join Date
    May 2002
    Posts
    74
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  4. #4
    MrExcel MVP DonkeyOte's Avatar
    Join Date
    Sep 2002
    Location
    Suffolk, UK
    Posts
    9,118
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.
    Does my a$$ look big in this picture ?

  5. #5
    Board Regular
    Join Date
    May 2002
    Posts
    74
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  6. #6
    MrExcel MVP DonkeyOte's Avatar
    Join Date
    Sep 2002
    Location
    Suffolk, UK
    Posts
    9,118
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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?

  7. #7
    Board Regular
    Join Date
    May 2002
    Posts
    74
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Is a stand alone computer with read only file.

    Rob

  8. #8
    MrExcel MVP DonkeyOte's Avatar
    Join Date
    Sep 2002
    Location
    Suffolk, UK
    Posts
    9,118
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  9. #9
    Board Regular
    Join Date
    May 2002
    Posts
    74
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

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

    Greetings,

    Rob

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com