+ Reply to Thread
Results 1 to 15 of 15

Variable in filename not increasing incrementally

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-15-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    141

    Variable in filename not increasing incrementally

    So, I have a macro that copies sheets to new files and saves the new files with incrementally increasing names. So, the first file would be "LocalizedData1", then second would be "...2", etc.

    I'm able to successfully save the first file, but later in the program, when the "pageCounter" variable has increased, the FName variable remains unchanged.

    
        Dim FName           As String
        Dim FPath           As String
      
     
        FPath = "C:\LocalizationOutput"
        FName = "LocalizedData" & pageCounter & ".xls"
     
         
        ThisWorkbook.Sheets("Localized" & pageCounter).Move Before:=Workbooks.Add.Sheets(1)
    
        Application.DisplayAlerts = False
        ActiveWorkbook.Sheets("Sheet1").Delete
        Application.DisplayAlerts = True
        
        If Dir(FPath & "\" & FName) <> "" Then
            MsgBox "File " & FPath & "\" & FName & " already exists. Exiting macro."
        Else
            ActiveWorkbook.SaveAs fileName:=FPath & "\" & FName
        End If

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Variable in filename not increasing incrementally

    I guess I don't see a loop or where you are incrementing pagecounter....
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Contributor
    Join Date
    02-15-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: Variable in filename not increasing incrementally

    That code is in a different section (it's a really long macro). I ended up putting this in again after the pageCounter increase: FName = "LocalizedData" & pageCounter & ".xls"

  4. #4
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Variable in filename not increasing incrementally

    Have you stepped (F8) through the code and opened the locals window....this will allow you to see the variable incrementing or not.....

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Variable in filename not increasing incrementally

    Does changing the pageCounter to Static help?
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  6. #6
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Variable in filename not increasing incrementally

    I'm wondering if the variable is global vs local....

    @mc84excel - is that what you mean by static?

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Variable in filename not increasing incrementally

    Declaring variables as static retains the variable value after the procedure containing the variable ends. See http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    How and where is the variable pageCounter being declared?

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Variable in filename not increasing incrementally

    Re your comment about global vs local. I learnt a lot from Chip Pearsons explanation of scope of variables - highly recommend it as a refresher. See http://www.cpearson.com/excel/scope.aspx

    (Resources I've read always identify 3 scope levels. Chip identifies 4 )

  9. #9
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Variable in filename not increasing incrementally

    @mc84excel - makes sense.....I learn them as Global vs local....either way I agree with your idea....

  10. #10
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Variable in filename not increasing incrementally

    @mc84excel

    Isn't that for VB and not VBA?

    Global and local is at what level you declare them. As in if they can be used by multple procedures, functions etc.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Variable in filename not increasing incrementally

    Quote Originally Posted by Solus Rankin View Post
    @mc84excel

    Isn't that for VB and not VBA?

    Global and local is at what level you declare them. As in if they can be used by multple procedures, functions etc.
    No, it's for VBA. Most books etc. describe three levels of scopes (local, module, global). Chip adds a 4th level 'Project'. (Project is like Global except that it can only be referenced within the same project. Whereas Global can be referenced from another open project).

  12. #12
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Variable in filename not increasing incrementally

    great idea - thanks

  13. #13
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Variable in filename not increasing incrementally

    Those aren't static. Static (and the link to MS you posted) are used in VB. I don't believe it functions the same way in VBA. At least I haven't found anything to show me it has.

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Variable in filename not increasing incrementally

    Quote Originally Posted by Solus Rankin View Post
    Those aren't static. Static (and the link to MS you posted) are used in VB. I don't believe it functions the same way in VBA. At least I haven't found anything to show me it has.
    Oh. I thought you were questioning the levels of scope - so I was answering that.

    Well I haven't used Static Variables myself in VBA but I have read about them in at least one * book on VBA ('Excel 2010 Power Programming with VBA' author John Walkenbach; See page 206)

    We are slightly getting off the OPs question.



    * at least one = I have a vague recollection of reading them elsewhere. Just can't remember at the moment.
    Last edited by mc84excel; 11-13-2013 at 07:29 PM.

  15. #15
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Variable in filename not increasing incrementally

    I don't see where pageCounter is declared. To trouble shoot we'd really need to whole code, or better yet an example workbook with code in place.

+ 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] Find if filename exists and update variable within the filename structure
    By Lungfish in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-18-2013, 02:42 AM
  2. Macro to copy from xls( variable filename) to static xls filename
    By gsp77 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2012, 09:33 AM
  3. Replies: 3
    Last Post: 07-09-2012, 03:09 PM
  4. increasing cell value with a variable
    By adiosboss in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2008, 11:29 AM
  5. [SOLVED] Converting a Variable Filename to a Constant Filename
    By Magnivy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2006, 01:15 PM

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