+ Reply to Thread
Results 1 to 13 of 13

Refering to a cell reference based on a variable

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Refering to a cell reference based on a variable

    Hi,

    I have defined Numrows as an integer.

    I want to use the following code :-

    Selection.AutoFill Destination:=Range("M6:M410")
    But rather than M410, I want the code to copy down to "numrows"

    How can I code this?

    Thanks in advance
    Last edited by bd528; 03-07-2010 at 04:59 AM.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Refering to a cell reference based on a variable

    Hi,

    You can try
    Selection.AutoFill Destination:=Range("M6:M"&Numrows)
    HTH

  3. #3
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Refering to a cell reference based on a variable

    Quote Originally Posted by JeanRage View Post
    Hi,

    You can try


    HTH
    When I tried that I got "method 'range' of object '_global' failed"

    Any other ideas?

    Thanks

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Refering to a cell reference based on a variable

    It's not clear what you're "filling" - formula/value etc... it's generally quicker to apply to a contiguous range using the below approach as opposed to AutoFill.

    Range("M6:M" & numrows).Formula = Selection(1).Formula
    If we assume numrows is 10 then M6:M10 will assume the formula of the first cell in the selection.

    If numrows is meant to represent the size of the vector rather than the literal end row number (implied by naming convention - ie number of rows) then you would use Resize

    Range("M6").Resize(numrows).Formula = Selection(1).Formula
    this would apply the formula to M6:M15 (10x1)

    If you're still having problems post a sample.

    (we assume the sheet is unprotected etc...)

  5. #5
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Refering to a cell reference based on a variable

    I have attached an example.

    I want to copy the forumula down to the last row (based on the number of entries in column A)

    I have used a seperate piece of code (not in the example) to count the number of entries in column A, which creates the integer "numrows". But if you know a better solution to counting them, please let me know.

    Thanks.
    Attached Files Attached Files

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Refering to a cell reference based on a variable

    Sorry, just looked at this...

    Sub Example()
    Range(Cells(5,"A"),Cells(Rows.Count,"A").End(xlUp)).Offset(,12).Formula = "=K5-L5"
    End Sub

+ 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