+ Reply to Thread
Results 1 to 4 of 4

'lastrow =' How long does statement 'work' in macro?

  1. #1
    Registered User
    Join Date
    03-17-2011
    Location
    Guelph, Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    63

    'lastrow =' How long does statement 'work' in macro?

    When a lastrow is specified:
    How long does it 'last'? Can the 'calculated' last row be used through-out the macro, now matter how long the macro is?
    Does a new lastrow statement 'over-ride' the original one", or does it just create confusion in the macro"
    Is there a way to 'de-active' a lastrow statement, so one can be sure it is not going to mess up the macro further on down?

    Please Login or Register  to view this content.
    Last edited by BEEJAY; 03-12-2012 at 08:44 AM.

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: 'lastrow =' How long does statement 'work' in macro?

    You are talking about scope of a variable. Within a macro the lastrow variable will remain in scope (that is retain it's value until the End Sub statement). If you want to change the lastrow value within the macro that is fine - for example
    Please Login or Register  to view this content.
    If you want to make lastrow available to multiple subs then declare it as a public variable - so it is declared OUTSIDE the sub
    Please Login or Register  to view this content.
    So the variable if declared as a public variable it will be available to all the macros - if the variable is declared within a sub the the scope is confined to the macro it is declared in and will not be available to other subs - that is the variable goes out of scope as soon as End Sub is reached and it's value will be lost and unavailable to other subs.
    In summary - The scope of a variable depends on where it is declared and how it is declared. You can change the value of lastrow as many times as you want - Excel does not suffer from confusion just because you assign a new value to the variable. Google Variable Scope in VBA and you will get more information.
    Is there a way to 'de-active' a lastrow statement, so one can be sure it is not going to mess up the macro further on down?
    If you no longer need to use lastrow in the macro there is no need to "deactivate it" which I assume you mean set lastrow = 0
    Please Login or Register  to view this content.
    In the VB editor there is an option to add a watch - highlight the variable and right click and select Add Watch - this will add the variable to the Watches window and you can see how the variable changes. When you go to a new sub you will see it will Go Out Of Context unless declared as a public variable. Instead of declaring a public variable you can also pass it to another sub e.g
    Please Login or Register  to view this content.
    So you don't have to declare the Public variable but just pass it as a parameter to another sub and this way the value of lastrow can be used in another sub without having to recalculate it. Hope that all made sense. As I said - Google variable scope in VBA - remember Google is your friend
    Last edited by smuzoen; 03-10-2012 at 09:39 AM.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: 'lastrow =' How long does statement 'work' in macro?

    Last row needs to be re-established if you change it.

    If the last row is determined to be 4
    Then you run code, such as a copy and paste to the first empty row.Then you want to repeat the process in the same code.

    The code will still think last row is 4, but really it is now 5. Unless you re-establish it again.

    Placement of LastRow is important
    Last edited by davesexcel; 03-10-2012 at 09:46 AM.

  4. #4
    Registered User
    Join Date
    03-17-2011
    Location
    Guelph, Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: 'lastrow =' How long does statement 'work' in macro?

    Smuzoen
    Thanks for all the detail on the subject.
    This will make it a lot easier to work with the 'lastrow'

    davesexel
    Thanks for pounding the point home.
    I've tripped myself up on this too many times.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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