+ Reply to Thread
Results 1 to 12 of 12

creating a loop for saving a file and increase a variable by one after every saving

  1. #1
    Forum Contributor
    Join Date
    08-03-2012
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    120

    creating a loop for saving a file and increase a variable by one after every saving

    Hi,
    I have this long but easy to understand vba code. How can I increase the z value by one after every round of saving the file? Right now I have to manually type in the numbers where it says z=4. I want the z value to increase up to 300 starting from 4. Also I want the last line (ActiveWorkbook.SaveAs Filename:=pathname & SaveFile & “M34.xlsb”) to be changed corresponding to the z value. So if z=4 then the file should be saved as M34. If z=5 then the file should be saved as M35 and so on. So M should be always saved as M30+z. thanks a lot for your help guys. I really appreciate it.


    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: creating a loop for saving a file and increase a variable by one after every saving

    Do you meant that the next time you run this code you want z to be 5, then next time 6, next time 7 etc?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: creating a loop for saving a file and increase a variable by one after every saving

    One way. On a separate sheet. In this sample I named it "z Values". In Column A you list the numbers down from 4 to 300.

    Please Login or Register  to view this content.
    In your sub

    z = Sheets("z Values").Range("A1").Value

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: creating a loop for saving a file and increase a variable by one after every saving

    If you want a loop.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-03-2012
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    120

    Re: creating a loop for saving a file and increase a variable by one after every saving

    Quote Originally Posted by Norie View Post
    Do you meant that the next time you run this code you want z to be 5, then next time 6, next time 7 etc?
    yes that is exactly what i want.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: creating a loop for saving a file and increase a variable by one after every saving

    Try this.
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    08-03-2012
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    120

    Re: creating a loop for saving a file and increase a variable by one after every saving

    somehow the loop doesnt work. do u know why

  8. #8
    Forum Contributor
    Join Date
    08-03-2012
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    120

    Re: creating a loop for saving a file and increase a variable by one after every saving

    norie. it does one loop and then stops.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: creating a loop for saving a file and increase a variable by one after every saving

    Which code are you using?

    The original code you posted didn't have a loop, the code I posted in post #4 has a loop and the code I posted in post #6 uses a static variable that will increment each time the code is run.

  10. #10
    Forum Contributor
    Join Date
    08-03-2012
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    120

    Re: creating a loop for saving a file and increase a variable by one after every saving

    hi,
    each time i run the code i get 34.xlsb. the static variable does not increase by one. As for the code in post #4 i get a syntax error for the line
    lr = Cells(Rows.Count, 1).End(xlUp).Row - 1

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: creating a loop for saving a file and increase a variable by one after every saving

    The code in post #4 adds the variable to the filename here.
    Please Login or Register  to view this content.
    Same for the code in post #6.

    As for the syntax error, I changed nothing in the original code apart from adding the loop (post #4) and static variable (post #6).

  12. #12
    Forum Contributor
    Join Date
    08-03-2012
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    120

    Re: creating a loop for saving a file and increase a variable by one after every saving

    i tried it many times. whenever i run the code #4 i get only one loop. it does not go from 4 to 300. It only does one loop and saves the file. the static variable in post #6 does also not work. whenver i run the #6 the variable does not increase by one. am i doing something wrong?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help with creating folder and saving file.
    By carden2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2013, 10:59 AM
  2. Converting file to PDF and Saving as Same Name In a Cell Loop
    By dash11 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2013, 02:24 PM
  3. Creating a .txt file extracting information from a Excel file saving as .resx file
    By AbdallahHajbed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2012, 09:00 AM
  4. Saving file with date variable
    By Steel Monkey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2006, 09:52 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1