+ Reply to Thread
Results 1 to 13 of 13

Refering to a cell reference based on a variable

  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 :-

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.
    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...

    Please Login or Register  to view this content.

  7. #7
    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

    Thanks for that - its nearly perfect.

    Please see my ammended attachment. I'd like L5 to remain blank if possible, after the macro is complete. I realise I could just use another piece of code to remove the current contents on the cell, but I assume you will know how to adjust your example.

    Thanks again.
    Attached Files Attached Files

  8. #8
    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

    Change the reference from 5 to 6

  9. #9
    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

    If I do that, I get #VALUE! in M6

  10. #10
    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

    I should perhaps have been more explicit

    Please Login or Register  to view this content.

  11. #11
    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 DonkeyOte View Post
    I should perhaps have been more explicit

    Please Login or Register  to view this content.
    No, not at all - I should have seen my mistake! Thank you thats great.

    As for my original question, (just so I know for future reference, and understand the syntax) in the code below, how can I refer to an integer variable row number, rather than 410?

    Please Login or Register  to view this content.
    Last edited by bd528; 03-06-2010 at 05:56 PM.

  12. #12
    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

    You would use something along the lines of:

    Please Login or Register  to view this content.
    Note: numrows should be declared as Long rather than Integer
    Long is advised when working with Row values given Row numbers can exceed Integer boundaries thereby generating overflow errors

    It is also assumed that numrows holds the last row number of interest rather than the count of rows to be included - if it is the latter then use a Resize based approach.

  13. #13
    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

    Thanks again. Exactly the information I needed.

+ 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