+ Reply to Thread
Results 1 to 5 of 5

Insert variables on R1C1 Formula

  1. #1
    Registered User
    Join Date
    11-27-2017
    Location
    Mexico
    MS-Off Ver
    2013
    Posts
    6

    Post Insert variables on R1C1 Formula

    Hey guys, I'm new here, I just looked over the whole internet and can't manage to understand what I'm going to explain and that probably is very simple.

    So, I have 4 variables:
    file1 = Sheets("BD MXN").Range("Y2")
    file2 = Sheets("BD MXN").Range("Y3")
    sheet1 = Sheets("BD MXN").Range("X4")
    sheet2 = Sheets("BD MXN").Range("X5")

    These variables are set on a specific cell because they change every month, so basically, before running the macro the user need to change some other cells to get to those variables (which are the file and sheet name of 2 different excel workbooks)
    Those variables won't change in the whole code, therefore, I want to insert them into a vlookup or [Index, Match (Inverse vlookup)] formula, for example (name of path is only to make it shorter):
    Range("K2").Select
    ActiveCell.FormulaR1C1 = _
    "=IFERROR(INDEX('C:\fullpath\[file1]sheet1'!R2C5:R30000C5,MATCH(RC[-9],'C:\fullpath\[file1]sheet1'!R2C6:R30000C6,0)),0)"

    So, I tried different syntax to make Excel write whatever it is filled on the variable, however, it writes literally the name of the variable or returns an error instead.

    Can someone help me figuring out this one? And an additional and great help would be if someone can explain me the logic on it (not just the code, but to understand what does VBA need to recognize a variable inserted in that way).

    Thank you very much in advance.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Insert variables on R1C1 Formula

    Untested in the basence of the workbook but perhaps...
    Please Login or Register  to view this content.
    I wasn't sure whether for instance 'file1' was a VBA variable that you have declared and set to take the value from Range Y2 in the BD MXN sheet.
    If so then replace the Sheets("BD MXN").Range("Y2") with file1
    Last edited by Richard Buttrey; 11-27-2017 at 02:02 PM.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    11-27-2017
    Location
    Mexico
    MS-Off Ver
    2013
    Posts
    6

    Re: Insert variables on R1C1 Formula

    Hello Richard,

    Thanks for the quick response, I changed it and it worked perfectly, thank you very much.

    and yes, 'file1' was a declared VBA variable that takes the value of Y2 in sheet BD MXN.

    So, one last question, whenever I have the need of doing that, should I give VBA the location of the text I'm willing to replace instead of providing a variable?

    Thank you very much

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Insert variables on R1C1 Formula

    Hi,

    To some extent it's a matter of personal choice, particularly if in your code there's only one or two references to the cell.
    Personally I always prefer to use a VBA variable for two reasons.

    1. I think it makes code easier to read
    2. There's a time overhead every time VBA needs to jump back to the Excel App and return. So it makes sense to create a VBA variable if it will be used many times. This is particularly relevant with looping procedures where within the loop a reference is made to a cell or object in the Excel App.

  5. #5
    Registered User
    Join Date
    11-27-2017
    Location
    Mexico
    MS-Off Ver
    2013
    Posts
    6

    Re: Insert variables on R1C1 Formula

    Thanks a lot for your time, greetings!

+ 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. Using Defined Variables in R1C1 Formula
    By nobodyukno in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-16-2017, 05:03 PM
  2. [SOLVED] Insert value in a cell using R1C1 formula in VBA syntax help
    By pongmeister in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-28-2017, 04:55 PM
  3. Insert value in a cell using R1C1 formula in VBA syntax help
    By pongmeister in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-28-2017, 11:32 AM
  4. Use of variables calculated in a R1C1 formula in VBA
    By Sousoudef in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2013, 11:06 AM
  5. R1C1 with variables and the Rank Function.
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2013, 11:24 PM
  6. Using named variables with R1C1 formatting?
    By jenlookxl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-14-2012, 07:44 PM
  7. [SOLVED] Trouble Including Variables in R1C1 VBA Code...
    By michaeljoeyeager in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-26-2012, 03:46 PM

Tags for this Thread

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