+ Reply to Thread
Results 1 to 9 of 9

struggling with lastrow formula

  1. #1
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    struggling with lastrow formula

    I found a few formulas online to find the lastrow of data in a workbook. The problem is I have pre-formatted 1500 rows to wrap text, borders, etc... so even though everything after row 100 is blank, the lastrow formulas I found online are returning 1500 as the lastrow instead of 100.

    The following formulas return 1500 as the last row:
    LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    LastRow = ActiveSheet.UsedRange.Rows.Count

    I found another formula online which seems to work in a test workbook I created, but in my workbook I'm getting an error:

    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    The error I get is "Object variable or With block variable not set". I do have it in a with statement though so I'm not sure why it's not working.

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: struggling with lastrow formula

    You will get this error if the worksheet is empty
    Gary's Student

  3. #3
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: struggling with lastrow formula

    Thanks for the feedback, but the worksheet is not empty. There must be another reason for this error occuring.

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: struggling with lastrow formula

    Post your workbook.

  5. #5
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: struggling with lastrow formula

    There are other activities going on in this workbook, but to replicate the error do the following:
    In the Main sheet click on the "Customer Search" button. Then hit the Run button in the Customer Search sheet and the debug prompt should appear.

    Keep in mind that I haven't written the part of code which will utilize this lastrow value.

    I appreciate the help.
    Attached Files Attached Files

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: struggling with lastrow formula

    I made two changes to the button code on the second sheet. From:

    Please Login or Register  to view this content.
    to:

    Please Login or Register  to view this content.
    and from

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    You can now continue your debugging.
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: struggling with lastrow formula

    Change the line to:

    Please Login or Register  to view this content.
    Adding the "." before Cells tells the code to search in Sheet "Opportunities" per the line above it (With Sheets("Opportunities")

    Let me know if this works.

  8. #8
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: struggling with lastrow formula

    Jakobshavn and BigBas, it works like a charm! Thank you both for helping me out with this.

  9. #9
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: struggling with lastrow formula

    Thanks for the feedback!

+ 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