+ Reply to Thread
Results 1 to 6 of 6

Auto-Fill formula to end of column, not end of sheet?

  1. #1
    Forum Contributor
    Join Date
    10-16-2012
    Location
    Brooklyn
    MS-Off Ver
    Excel 2010 PC
    Posts
    137

    Auto-Fill formula to end of column, not end of sheet?

    Hi.
    I'm trying to auto-fill a formula in two columns of a worksheet. I've gotten it to work by making the range end in a number, i.e., "M2:M1001." However, this leaves ugly "#####'s" all the way down to row 1001 on sheets with a lot less than 1001 rows. And here in lies the problem.

    I've been unsuccessfully trying to use a row count to get the last row of the column and use it in the formula. I'd really like my auto-fill to end on the last row of the column. Is there a way I can do this?

    This is the code I've been tinkering with.

    Please Login or Register  to view this content.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Auto-Fill formula to end of column, not end of sheet?

    The arguments to Cells are (row, column). They must be expressions that evaluate to numbers; you can't use "M" as a column indicator. Replace it with 13. Same for "N".

    For Range, you have to use a string that gives a range, such as "M1:M1001" (there are other forms as well but let's just limit ourselves to your code). Anything that you put in there you would also be able to use in a worksheet formula like SUM. However, you are using the string "M2:LastRow_M", which makes no sense to Excel. When you put your variable name inside the quotes, it becomes part of a string rather than the value of the variable. You have to build the string using LastRow_M as a number. If you use

    "M1:M" & LastRow_M

    it will build a string that looks like

    M1:M1001

    See changes below.

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Auto-Fill formula to end of column, not end of sheet?

    You appear to be trying to do the right thing in that you define and use LastRow_M and LastRow_N. However, you use LastRow when you set the value(s).

    And don't use .Select ... Selection.; you should combine those lines.

    This:

    Please Login or Register  to view this content.

    Could be:

    Please Login or Register  to view this content.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Auto-Fill formula to end of column, not end of sheet?

    Thanks for the rep.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Auto-Fill formula to end of column, not end of sheet?

    TMShucks had a good catch that I missed. Just for completeness, second round of changes in blue:

    Please Login or Register  to view this content.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Auto-Fill formula to end of column, not end of sheet?

    @6SJ: Well, I missed the whole Range("M2:M" & LastRow_M) bit so I think we're quits

+ 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