I am trying to pass a range reference to a vlookup formula using a function. Below is the vlookup formula with the mytrim function:
Selection.Formula = "=vlookup(D1, sheet1!A:" & mytrim(Range("XFD1").End(xlToLeft).Address) & ",2,0)"
Here's the code of my mytrim function:
Public Function mytrim(str As String) As String
Dim x
Dim y
x = InStr(1, str, "$")
y = InStr(x + 1, str, "$")
mytrim = Left(Replace(str, "$", ""), y - x - 1)
End Function
At the execution of end function step error is thrown "Object Variable or with block variable not set"
I am trying to get the vlookup formula as "=vlookup(D1,sheet1!A:AE,2,0)". AE being the last column in sheet1 which might change.
mytrimequal what you expected? Could you put a Watch onx,yandmytrimand confirm everything is evaluating as expected?Selection? I think that is your problem. I'm guessing it isNothingor something else odd