+ Reply to Thread
Results 1 to 4 of 4

R1C1 Vlookup with Variables to reference column numbers

  1. #1
    Registered User
    Join Date
    12-16-2020
    Location
    Washington
    MS-Off Ver
    Office 365
    Posts
    3

    Question R1C1 Vlookup with Variables to reference column numbers

    Hello

    I am having some issues with using variables in FormulaR1C1 format and would appreciate any assistance.

    This formula works perfectly for me:

    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-3],[ExampleBook.xlsx]Wide!C1:C5,5,FALSE)"

    I am trying to have the C5 and 5 be generated using a variable depending on the value from another cell, so I create this:

    Dim RndRef As Long
    RndRef1 = Range("$L$10").Value

    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-3],[ExampleBook.xlsx]Wide!C1:C"&RndRef1&",&RndRef1,FALSE)"

    Which gives me a "Compile error: Expected: end of statement". I have tried utilizing many suggestions I have seen while trying to fix the error, and they all show the same error.

    I am primarily trying to cut back on copying all of my code and adjusting to generate 8 separate files that I am pulling data in from a completely separate file. I have all my code created with the exception of this one error, any help would be greatly appreciated.

  2. #2
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: R1C1 Vlookup with Variables to reference column numbers

    Note that RndRef and RndRef1 are different variables. I'll assume the Dim statement is a typo.

    The problem is

    "=VLOOKUP(RC[-3],[ExampleBook.xlsx]Wide!C1:C"&RndRef1&",&RndRef1,FALSE)"

    That should be

    "=VLOOKUP(RC[-3],[ExampleBook.xlsx]Wide!C1:C"&RndRef1&","&RndRef1&",FALSE)"

    That said, I need to do this sort of thing often, and the approach I take is like this.

    Const FORMULATEMPLATE As String = "=VLOOKUP(RC[-3],[ExampleBook.xlsx]Wide!C1:C#,#,FALSE)"
    ':
    ActiveCell.FormulaR1C1 = Replace(FORMULATEMPLATE, "#", RndRef1)

  3. #3
    Registered User
    Join Date
    12-16-2020
    Location
    Washington
    MS-Off Ver
    Office 365
    Posts
    3

    Re: R1C1 Vlookup with Variables to reference column numbers

    It was indeed a typo.

    Thank you for the information, I am going to give the Const FORMULATEMPLATE route a try since me just trying to make the change you are indicating
    won't let me leave the line without the error popping up at at the &","& location in the formula. I have attached the screenshot with all my primary identifiers blurred out.

    If you have any other thoughts please let me know, and I'll let you know if the other route works for me!

    Thank you!!
    Attached Images Attached Images

  4. #4
    Registered User
    Join Date
    12-16-2020
    Location
    Washington
    MS-Off Ver
    Office 365
    Posts
    3

    Re: R1C1 Vlookup with Variables to reference column numbers

    That Const FormulaTemplate route worked like a charm!

    Thank you so much for your advice!

+ 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] Using variables in a R1C1 statement
    By senior citizen sue in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-15-2018, 02:24 PM
  2. Insert variables on R1C1 Formula
    By ccasio04 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2017, 03:34 PM
  3. Negative relative reference in R1C1 Formula inside vlookup
    By contactfornitish in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-14-2016, 04:39 PM
  4. [SOLVED] Formula R1C1 reference a column
    By Journeyman3000 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-13-2014, 08:39 PM
  5. R1C1 cell reference in Vlookup...with wildcard?
    By musicman715 in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 08-30-2013, 01:45 PM
  6. 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
  7. 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

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