+ Reply to Thread
Results 1 to 11 of 11

Second Last Row in VB

  1. #1
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Second Last Row in VB

    Hi,

    I have created a Print button and now i want to set a marco to it, so that it will only print the specific range.

    The Range I want to Print is B2:M46, however M46 can change if new lines are inserted. From row 48 onwards I have hidden so my last visible row is row 48.

    So for range to select i was thinking B2:Lastrow-2. But I am not sure how to select Last Row -2 and then assign print properties to it.

    Any help is much appriciated.

    Thankyou

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

    Re: Second Last Row in VB

    Maybe this. I wasn't sure if you wanted to set up a Print Area so I added it. You could just as easily say myRange.PrintOut

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: Second Last Row in VB

    hi tharindudk

    when you say last row, do you mean the actual last row in the spreadsheet. This snippet will get you the last row of the nominated spreadsheet tab, you can then adjust the value

    Please Login or Register  to view this content.
    Jmac

  4. #4
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Second Last Row in VB

    Hi, This is exactly what I want to do, However I get an object defined error in line

    Please Login or Register  to view this content.
    Do you know why I would get that?

    Thanks.

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

    Re: Second Last Row in VB

    Nope. It works just fine for me on a test workbook. I would need to see you workbook to be able to tell you for sure. More than likely its an error on your side where you said column M but you really meant column L. Or you didn't have any data in column M when you tried the code.

  6. #6
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Second Last Row in VB

    Hi stnkynts,
    Yes I don't have any Data in column M. Its just a border for the page.

    Do I need to have data there?

    Thanks

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Second Last Row in VB

    Hi tharindudk,

    Try this where if there's no data the 'lngMyRow' variable will be set to 2 (change to suit):

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  8. #8
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Second Last Row in VB

    Hi, now the range comes up as B2:M38.

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Second Last Row in VB

    So the last row from columns B to M (inclusive) must be 40

  10. #10
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Second Last Row in VB

    The Range I want to Print is B2:M46, however M46 can change if new lines are inserted. From row 48 onwards I have hidden so my last visible row is row 48.

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Second Last Row in VB

    Regardless if the row is hidden or not the last row to have data across columns B to M is 40.

    Try this where the 'lngMyRow' variable is based on the first hidden row (if there is one):

    Please Login or Register  to view this content.
    HTH

    Robert

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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