+ Reply to Thread
Results 1 to 5 of 5

Using Variable in Cell Formula as part of For Next

  1. #1
    Registered User
    Join Date
    01-03-2005
    Posts
    35

    Using Variable in Cell Formula as part of For Next

    I have 2 sheets, Sheet1 and Sheet2

    I want to populate Sheet2 Column B with a formula which will reference Column B of sheet1 so that on each iteration of the For..Next, the cell being referenced in the formula will change.

    So if i = 5, I'd like the cell to be populated with the formula
    =Sheet1!B5


    So I have the following code
    Please Login or Register  to view this content.

    However, something clearly isn't working right.


    - Firstly, from what I can see, the FormulaR1C1 takes the numbers in R[] and C[] as relative offsets. So R[1] if I'm in row 1, would point to row 2. Is my understanding here correct? And if so, how to I get it to just point to row i based on the variable value?

    - Secondly, there is something else wrong in what I've done in that the cells this macro is populating column B with the same formula in every cell (so each cell in column B is pointing to B13)

    "=Sheet1!B13"
    Last edited by grey; 08-13-2009 at 11:36 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using Variable in Cell Formula as part of For Next

    To get an R1C1 style reference to refer to the same row you're in, simply omit any number after the R. Also, taking the brackets off of the numbers you do use will make the references absolute.

    R1 - always means row 1
    R[1] - always means 1 row below where this formula is located
    R[-1] - 1 row above
    R - means "same as this row"

    If you're looping through rows, you can just use the R by itself since it's rows you're dealing with, most likely the formula you're inserting is meant to apply to this row, yes?

    SIDE NOTE:
    In declaring variables, go ahead and get into the habit of declaring row variables as LONG, not INTEGER. Even in Excel 2003 there are more rows on a sheet than an INTEGER variable can hold, LONG is the safer choice for row/column variables.
    Last edited by JBeaucaire; 08-13-2009 at 10:19 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

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

    Re: Using Variable in Cell Formula as part of For Next

    Might be an idea to clarify how you're defining lastrow variables as I suspect you can achieve this without need for iteration.

  4. #4
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: Using Variable in Cell Formula as part of For Next

    HI
    try the following codes
    Please Login or Register  to view this content.
    Ravi

  5. #5
    Registered User
    Join Date
    01-03-2005
    Posts
    35

    Re: Using Variable in Cell Formula as part of For Next

    Thanks for all of your help, you guys rule! :D

+ 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