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:**

```
Sheets("Count").Activate
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.")
Else
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)
Sheets("Input").Activate
Dim xrnge As Range, s
Set xrnge = Range(startcells, Cells(endrow, lcol))
s = xrnge.Address
Sheets("Count").Activate
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:**

```
=IFERROR(VLOOKUP($B20;Input!s;1;0);"")
```

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

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:

```
=IFERROR(VLOOKUP($B8,Input!$S$3:$S$50,1,0),"")
```

Which will be a working formula as expected.

Tags: dynamic, excelexcel