Home » excel » excel – Vlookup dynamic range reference from a different sheet

excel – Vlookup dynamic range reference from a different sheet

Posted by: admin May 14, 2020 Leave a comment


I am inserting a vlookup formula in a range of cells. The formula however has a dynamic reference in it, and I am having hard time in constructing it correctly.

Here is my code:


Dim myValue As Date
myValue = InputBox("Please enter the date you want to update")
Sheets("Count").Range("A1").Value = myValue

Dim ra As Range, raEnd As Range
Dim date1 As Date
date1 = Range("A1")

Set ra = Cells.Find(What:=date1 _
                    , LookIn:=xlFormulas _
                    , LookAt:=xlPart _
                    , SearchOrder:=xlByRows _
                    , SearchDirection:=xlNext _
                    , MatchCase:=False _
                    , SearchFormat:=False)

    If ra Is Nothing Then
    MsgBox ("Date not found, check the format.")


        Dim Lastrow As Long, rng As Range
        Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
        Set rng = Range(ra.Offset(1, 0), Cells(Lastrow, ra.Column))

        Dim endrow As Long, i As Long, lcol As Integer, startcells As Range
        lcol = Sheets("Count").Range("B1").Value
        endrow = Sheets("Input").Cells(Rows.Count, lcol).End(xlUp).Row
        i = 6
        Set startcells = Sheets("Input").Cells(i, lcol)


        Dim xrnge As Range, s
        Set xrnge = Range(startcells, Cells(endrow, lcol))
        s = xrnge.Address

        rng.Formula = "=IFERROR(VLOOKUP($B8,'" & "input" & "'!s,1,0),"""")"

    End If

End Sub

The part that I cannot make work is

rng.Formula = "=IFERROR(VLOOKUP($B8,'" & "input" & "'!s,1,0),"""")"

I think I am almost there because in the range where I want to insert the formula looks like this:


Could you help me to figure out why it shows s and not the range itself?

How to&Answers:

If I understand correctly you want s to hold a range address, and input to be a static sheet reference. In that case, you should write your formula as so:

rng.Formula = "=IFERROR(VLOOKUP($B8,input!" & s & ",1,0),"""")"

Say s holds the address for range S3:S50 the result in cell will be:


Which will be a working formula as expected.