+ Reply to Thread
Results 1 to 4 of 4

Using variable in FormulaR1C1

Hybrid View

amphinomos Using variable in FormulaR1C1 06-02-2015, 11:54 AM
Norie Re: Using variable in... 06-02-2015, 11:57 AM
MrShorty Re: Using variable in... 06-02-2015, 11:58 AM
amphinomos Re: Using variable in... 06-02-2015, 12:01 PM
  1. #1
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Using variable in FormulaR1C1

    Hi,

    I'm getting a "Run-time error 1004 : Application-defined or object-defined error" when trying to use variables in a R1C1 formula.
    This works fine
    Sheets(1).Cells(3, 1).FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
    but this generates an error
    Dim i as integer
    Dim j as integer
    
    i = -2
    j = -1
    
    Sheets(1).Cells(3, 1).FormulaR1C1 = "=SUM(R[i]C:R[j]C)"
    Is it not possible to use variables here ? If so, are there any workarounds ? I would like VBA to generate an actual formula in the cell and not the result.

    Thanks,
    amphi

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Using variable in FormulaR1C1

    Try this.
    Dim i as integer
    Dim j as integer
    
    i = -2
    j = -1
    
    Sheets(1).Cells(3, 1).FormulaR1C1 = "=SUM(R[" & i & "]C:R[" & j & "]C)"
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,462

    Re: Using variable in FormulaR1C1

    You need to use the concatenate operator (&) to combine the text literals and the variables into the final formula text string:
    "sum(r[" & i & "]c:r[" & j & "]c)". You can see that there are 5 individual text strings that concatenate together to form the final text string assigned to the formular1c1 property.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Using variable in FormulaR1C1

    Don't understand why this works but thanks Norie !

    Edit: now I do. Thanks MrShorty !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] FormulaR1C1 - Subtracting 30 days from variable date
    By Gard5096 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-20-2013, 12:19 PM
  2. [SOLVED] Using FormulaR1C1 with a Variable
    By Gard5096 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2013, 06:24 PM
  3. FormulaR1C1 and Variable.
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-24-2012, 07:23 AM
  4. FormulaR1C1 value with variable calc
    By degelo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-15-2011, 11:54 AM
  5. FormulaR1C1 and variable references
    By suburbanght in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-10-2007, 12:30 PM

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