+ Reply to Thread
Results 1 to 9 of 9

Keeping a lastrow Variable Across Modules

  1. #1
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Keeping a lastrow Variable Across Modules

    Hello Everyone,

    I looked around the internets and couldn't find much of an answer for this - I'm trying to make it so that a lastrow I defined in one module will still be defined in another macro.

    My background for this - my macro is too large for one module, therefore, I split it in two. However, the new module uses defined variables (multiple lastrow definitions) from the original module.

    So, is there a way to make it so when I originally define a lastrow in one module, other modules will recognize it?

    Thanks in advance!

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Keeping a lastrow Variable Across Modules

    One way:

    Please Login or Register  to view this content.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Keeping a lastrow Variable Across Modules

    Hi,

    Define it as a Public Variable outside of any procedure.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Keeping a lastrow Variable Across Modules

    stnkynts (i'm glad the old man let you off the back of the truck, btw) and richard,

    I apologize that I can't copy the macro as an example (work restrictions) but is there a way to do this within the macro? The reason I'm not just redefining the lastrow in module2 is because multiple changes are made to the worksheet after it is defined (rows are added, some with data and some blank).

    Thanks again!

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Keeping a lastrow Variable Across Modules

    That is what declaring it as public is doing. Here is some further clarification/ working proof of the same thing I showed earlier.

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Keeping a lastrow Variable Across Modules

    stnkynts - I think I have it right now, but do I have to make every lastrow that I have set a new module with this? Is there any way to make it so the rest of them don't need to be set the same way?

    Thanks!

  7. #7
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Keeping a lastrow Variable Across Modules

    I'm not quite sure what you mean.

    Any variable that you want accessable across modules needs to be publicaly declared. If the variable value needs to be updated just update the value within the code at the point which you need.

  8. #8
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Keeping a lastrow Variable Across Modules

    Okay, here's an example of what I'm talking about - I have about 10 other lastrow variables out there. Let's say one of them is only in one module, will I still need to make that public and enter the the code you provided above?

    Thanks

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Keeping a lastrow Variable Across Modules

    Hi,

    If it helps to understand this, in essence there are two important concepts associated with variables, Scope and Lifetime. The scope defines which procedures can use the variable the lifetime defines how long the variable retains its value.

    If you declare a variable within a procedure with Dim then the variable when set lasts only as far as the End Sub of the procedure.

    If you declare a variable at the Module Level, i.e. outside a procedure at the top of a Module with Dim, (or Private which is equivalent), then the variable when set retains its value for all procedures in that module.

    If you declare a variable at the Module Level with Public then the variable when set is available to any procedure in any of the modules.

    Note it follows from the above that you can declare procedure level variables with the same name (although it's not usually good practice), but as far as VBA is concerned they are quite independent. i.e If you Dim say 'Sales' and set it to 100 in procedure 1, then Dim 'Sales' in procedure 2 and set it to 200 and each procedure will continue to use its own value.

    To answer your question it depends on the where you want the lastrow variable to be visible and whether you have more than one lastvariable each different.

+ 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. Passing a variable between modules in VBA
    By MikeFranz123 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-03-2013, 08:20 AM
  2. [SOLVED] Incorporate the value of a LastRow variable in a VBA code/formula
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2012, 11:05 PM
  3. Variable to retain its value for other modules
    By stoney1977 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-04-2008, 10:51 AM
  4. Defining one variable for all modules
    By jasoncw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2007, 03:11 PM
  5. Making a variable in userform available in modules
    By gj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2006, 03:05 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