Results 1 to 3 of 3

Thread: Cell Value to become part of RC in macro

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    7
    Rep Power
    0

    Cell Value to become part of RC in macro

    Hi guys! I have a slight conundrum. I have a cell that changes dynamically. This number is needed within a macro's "RC" reference.

    If the cell is 3, the reference in the macro should become RC3.

    Any insight will be appreciated!

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,521
    Rep Power
    10
    Hi
    Here is a simple example: in cell P24 I have the formula
    = O24
    In RC stuff that would be like in a macro to put it in
    Code:
    Sub Macro4()
     Let Range("P24").FormulaR1C1 = "=RC[-1]"
    End Sub
    Lets say in N24 I had -1
    So then this macro would do the same as the last one
    Code:
    Sub Macro4()
     Let Range("P24").FormulaR1C1 = "=RC[" & Range("N24").Value & "]"
    End Sub
    _____ Workbook: macro.xlsm ( Using Excel 2007 32 bit )
    Row\Col
    N
    O
    P
    24
    -1
    0
    Worksheet: macro_xlsmSh1efJ20

    _____ Workbook: macro.xlsm ( Using Excel 2007 32 bit )
    Row\Col
    N
    O
    P
    24
    -1
    =O24
    Worksheet: macro_xlsmSh1efJ20

    _____ Workbook: macro.xlsm ( Using Excel 2007 32 bit )
    Row\Col
    N
    O
    P
    24
    -1
    =RC[-1]
    Worksheet: macro_xlsmSh1efJ20

    To do it a bit better, you should always qualify the worksheet, for example in that last case I had a worksheet of macro_xlsmSh1efJ20 and File of macro.xlsm
    Code:
    Sub Macro4()
     Let Workbooks("macro.xlsm").Worksheets("macro_xlsmSh1efJ20").Range("P24").FormulaR1C1 = "=RC[" & Workbooks("macro.xlsm").Worksheets("macro_xlsmSh1efJ20").Range("N24").Value & "]"
    End Sub
    Or to keep it a bit neater, other ways
    Code:
    Sub Macro4()
        With Workbooks("macro.xlsm").Worksheets("macro_xlsmSh1efJ20")
         Let .Range("P24").FormulaR1C1 = "=RC[" & .Range("N24").Value & "]"
        End With
    End Sub
    Code:
    Sub Macro4()
    Rem Worksheets info
    Dim Ws As Worksheet, Wb As Workbook
     Set Wb = Workbooks("macro.xlsm")
     Set Ws = Wb.Worksheets("macro_xlsmSh1efJ20")
    Rem Do it
     Let Ws.Range("P24").FormulaR1C1 = "=RC[" & Ws.Range("N24").Value & "]"
    End Sub
    The last way is probably the best one to get in the habit of using




    Just to try to make it clear.
    in your coding, you would usually## need to replace the number with like
    " & Ws.Range("N24").Value & "
    So in your case you would replace
    3
    with something like
    " & Ws.Range("B1").Value & "
    assuming in this examplke that your number 3 was in cell B1

    ##It might sometimes be a bit more complicated then that, depending on exactly what your code line containing the RC3 looks like


    Alan
    Last edited by DocAElstein; 07-18-2020 at 05:14 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    7
    Rep Power
    0
    Doc you are an absolute legend!!!

    I used your first suggestion!

    Code:
    Sub Macro4()
     Let Range("P24").FormulaR1C1 = "=RC[-1]"
    End Sub
    Worked right off the bat. Thanks so much!

Similar Threads

  1. Replies: 4
    Last Post: 12-31-2014, 03:59 AM
  2. Replies: 2
    Last Post: 05-30-2013, 07:28 PM
  3. Replies: 2
    Last Post: 04-16-2013, 01:36 PM
  4. suggest the mistake in this macro part
    By Safal Shrestha in forum Excel Help
    Replies: 8
    Last Post: 04-03-2013, 11:57 AM
  5. Replies: 5
    Last Post: 03-09-2013, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •