+ Reply to Thread
Results 1 to 9 of 9

error on lastrow for multiple columns

  1. #1
    Registered User
    Join Date
    04-08-2015
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    51

    error on lastrow for multiple columns

    I'm trying to write some listbox values to variable worksheets (product name from listbox = worksheet name).

    The following code returns an error 13 type mismatch on the lastRow = ws.Columns("b:d").Find("*", ws.Cells(1), , , 1, 2).Row + 1 part.

    Please Login or Register  to view this content.
    I used this lastro formula in another part of my workbook where it works great (also see this topic)

    Basically the columns that need to be checked for a value are B-C-D.


    Could anyone please help me out?
    Thanks in advance.

  2. #2
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: error on lastrow for multiple columns

    Your issue may possibly be solved by simply declaring: Dim lastrow As Long.

    I don't see a variable declaration for lastrow. Is it a global variable or are you using late binding (in other words, did you declare: Option Explicit on the top of your module, which is recommended)?

    Another thing that I would check is that if whether or not lastrow is declared as an integer or a Long (upon late binding by looking at the Locals window in the VBA editor, or elsewhere). Many variables that are declared as integers are not large enough to store the last row of an excel document and must be declared as Long.
    <---If my answer helped, please click *

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

    Re: error on lastrow for multiple columns

    Hi pclaesen,

    Though I'm sure jindon's code is just fine, this is what I normally use:

    Please Login or Register  to view this content.
    This will work as long as there's actually some data in the ws tab (though I can't see where you've declared a 'ws' variable as a worksheet ) in any column the code is referencing - B to D in this case. If there's not a Run-time error 91 error message will result.

    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

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

    Re: error on lastrow for multiple columns

    Your issue may possibly be solved by simply declaring: Dim lastrow As Long.
    No the problem still occurs I'm afraid

    Another thing that I would check is that if whether or not lastrow is declared as an integer or a Long (upon late binding by looking at the Locals window in the VBA editor, or elsewhere). Many variables that are declared as integers are not large enough to store the last row of an excel document and must be declared as Long.
    You will get a different error i.e. Run-Time Error '6' Overflow in this case when the maximum limit an integer can hold 32,767 is exceeded by a higher row number.

  5. #5
    Registered User
    Join Date
    04-08-2015
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    51

    Re: error on lastrow for multiple columns

    Quote Originally Posted by Trebor76 View Post
    Hi pclaesen,

    Though I'm sure jindon's code is just fine, this is what I normally use:

    Please Login or Register  to view this content.
    This will work as long as there's actually some data in the ws tab (though I can't see where you've declared a 'ws' variable as a worksheet ) in any column the code is referencing - B to D in this case. If there's not a Run-time error 91 error message will result.

    HTH

    Robert
    Hi Robert,

    the worksheet name (variable) is the productcode I enter in the userform and listbox:

    Please Login or Register  to view this content.
    Your code works, thank you very much! I've been searching for nearly 4h on this :-D.
    Last edited by pclaesen; 01-21-2016 at 05:53 AM. Reason: typo

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

    Re: error on lastrow for multiple columns

    the worksheet name (variable) is the productcode I enter in the userform and listbox:
    Without declaring ws as a worksheet variable it's being declared as a variant - the most expensive type of variable in terms of memory.

    Your code works, thank you very much! I've been searching for nearly 4h on this
    I'm glad to hear that and you're welcome

    Robert

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

    Re: error on lastrow for multiple columns

    the worksheet name (variable) is the productcode I enter in the userform and listbox:
    Without declaring ws as a worksheet variable it's being declared as a variant - the most expensive type of variable in terms of memory.

    Your code works, thank you very much! I've been searching for nearly 4h on this
    I'm glad to hear that and you're welcome Thanks also for the rep

    Robert

  8. #8
    Registered User
    Join Date
    04-08-2015
    Location
    Belgium
    MS-Off Ver
    Office 2013
    Posts
    51

    Re: error on lastrow for multiple columns

    Quote Originally Posted by Trebor76 View Post
    Without declaring ws as a worksheet variable it's being declared as a variant - the most expensive type of variable in terms of memory.



    I'm glad to hear that and you're welcome Thanks also for the rep

    Robert
    Robert,

    So I have to (just brainstorming here):

    Please Login or Register  to view this content.
    to speed up things?

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

    Re: error on lastrow for multiple columns

    Declaring all your variables will improve efficiency but there maybe other factors at play if the performance is slow.

    For the ws variable you'd use this:

    Please Login or Register  to view this content.
    Robert

+ 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] LastRow - for next loops longer than lastrow?
    By flabb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-07-2016, 12:39 PM
  2. Get LastRow based on a series of columns ranges?
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-21-2014, 02:23 PM
  3. [SOLVED] VBA Find LastRow Of Multiple Non Contiguous Columns On One SHeet
    By hobbiton73 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-08-2014, 01:20 PM
  4. LASTROW func leading to #REF! error?
    By Mvaldesi in forum Excel General
    Replies: 4
    Last Post: 10-07-2011, 08:43 AM
  5. VBA -autofill using LastRow error when only 1 record present
    By bk77 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2010, 06:47 PM
  6. lastrow of 3 columns
    By Directlinq in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2009, 10:35 AM
  7. error on line For i = 1 To LastRow.Row - 1 Step 2
    By coolhit in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-14-2009, 01:01 AM

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