+ Reply to Thread
Results 1 to 7 of 7

Using a relative cell reference in a function

  1. #1
    Registered User
    Join Date
    05-23-2007
    Posts
    4

    Using a relative cell reference in a function

    Hi, apologies in advance for what I'm sure is a too-simple question, but I can't work this out!

    I want to use a relative cell reference in a function - so say the function is used by cell A1 and I want to return the value of cell A2. If I make the function:

    test = Worksheets("Sheet1").Range("A2").Value

    then it works. But if I use

    test = Worksheets("Sheet1").Range("RC[1]").Value

    then it doesn't - it returns #VALUE!

    Can anyone help me out with the correct syntax?


    Thanks

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445
    Hi John,

    I'm not sure exactly what you're after doing, but you might want to try the .offset method

    Please Login or Register  to view this content.
    will return the value of A1, offset by 1 row and 0 columns to the variable test
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    05-23-2007
    Posts
    4
    Dave - What I want is to refer to the function from a whole load of different cells, so I can't use any absolute references. basically I want the function to know which cell is calling it, and be able to return a value from a cell relative to it.

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445
    in the case of a user defined function, the cell calling the function is given with the ActiveCell object eg.,

    Please Login or Register  to view this content.
    and you can refer to cells relative to this, as discussed, with the offset method. eg.,

    Please Login or Register  to view this content.
    Dave

  5. #5
    Registered User
    Join Date
    05-23-2007
    Posts
    4
    Thanks Dave, but I still can't get it to work - the activecell reference seems very strange.

    I attach a small demo to show where I'm at.
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Use Caller

    Hi John

    For this you need to use Application.Caller so that the cell from which you offset is the worksheet cell that contains the function ie:

    Please Login or Register  to view this content.
    Is this for a more complicated function (ie this is just an example)? I can't see a great deal of use for this function.

    Best regards

    Richard

  7. #7
    Registered User
    Join Date
    05-23-2007
    Posts
    4
    Richard - thank you, that's just what I needed!

    Now that I've got the syntax right I will be making more complex use of it, yes.

    Thanks


    John

+ 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