In continuation of this question by Bluesector:
He was looking for a way to basicly do a VLookup()
to place an image in a cell below. I was working on an answer using a user defined function (UDF).
I wrote the following function that:
- Takes a range as input variable. That Range should contain the value that is to be looked up.
- Finds a match on the worksheet
"PIC"
and copies that cell. - Pastespecial a linked picture to the active cell (the cell it has been called from)
Function insertIMG(ByVal rng As Range)
Dim row As Integer
row = Application.WorksheetFunction.Match(rng, Sheets("PIC").Range("A1:A5"), 0)
Sheets("PIC").Range("B" & row).Copy
With Worksheets("Blad1")
'adapt worksheet name as appropriate
.Pictures.Paste(Link:=True).Select
End With
insertIMG = ""
End Function
Which results in a #Value error when inserted in a cell on a workbook.
When I run this code from within a sub like so:
Sub test()
Application.Workbooks("Map2").Worksheets("Blad1").Range("D5").Select
insertIMG (Application.Workbooks("Map2").Worksheets("Blad1").Range("D4"))
'adapt workbook and worksheet name as appropriate
End Sub
it works as expected. In this I explicitly select cell D5, as that would be where the function is called from, and would thus have the active selection when used in an UDF.
Is there any limitation to the UDF's that I'm missing here?