How to get VLOOKUP to select down to the lowest row in VBA?

后端 未结 2 1622
逝去的感伤
逝去的感伤 2021-01-27 14:12

Looking to automate the insertion of a VLOOKUP formula in a cell. When recording the macro I instruct it to populate the columns below with the same formula. Works great, howev

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-27 14:33

    try this:

    With Worksheets("Previous")
        Range("AJ2").FormulaR1C1 = _
            "=VLOOKUP(RC[-20], Previous!R2C2:R" & .Cells(.Rows.Count, 2).End(xlUp).Row & "C22,17,FALSE)"
    End With
    

    where:

    • Range("AJ2")

      will implicitly reference the ActiveSheet

    • .Cells(.Rows.Count, 2).End(xlUp).Row

      will reference "Previous" worksheet, being inside a With Worksheets("Previous")- End With block

提交回复
热议问题