I've run into a dead-end on this one, and I'm quite a novice when it comes to VBA. I've got a user defined function which is essentially a vlookup, but it returns cell comments also. I got this function from someone on this board a long time ago, but I'm sorry I don't know who to give the credit to. Anyway, the function is below:
I'm trying to use named ranges in this function as in:
This method works great when using a regular vlookup function, but not with the user defined function. My end goal is to pull user input from one spreadsheet to a new, updated spreadsheet, I'm trying to make it somewhat user-proof so it can deal with a user adding or deleting columns.
I have considered using a loop along the lines of:
Each time through the loop would take the next cell in the named ranges, but I don't know how to make this happen. Any suggestions on how to make this UDF work with named ranges? Thanks in advance!!
Code:
Option Explicit
Function VlookupComment(LookupValue As Variant, LookupTable As Range, _
ColumnNumber As Long, MatchType As Boolean) As Variant
Application.Volatile True
Dim res As Variant 'could be an error
Dim myLookupCell As Range
res = Application.Match(LookupValue, LookupTable.Columns(1), MatchType)
If IsError(res) Then
VlookupComment = "Not Found"
Else
Set myLookupCell = LookupTable.Columns(ColumnNumber).Cells(1)(res)
VlookupComment = myLookupCell.Value
With Application.Caller
If .Comment Is Nothing Then
'do nothing
Else
.Comment.Delete
End If
If myLookupCell.Comment Is Nothing Then
'no comment, do nothing
Else
.AddComment Text:=myLookupCell.Comment.Text
End If
End With
End If
End Function
I'm trying to use named ranges in this function as in:
Code:
ActiveCell.FormulaR1C1 = "=VLOOKUPCOMMENT(NamedRange1,NamedRange2,2,FALSE)"
I have considered using a loop along the lines of:
Code:
dim c as range
for each c in range ("NamedRange1")
c.FormulaR1C1 = "=VlookupComment(first cell of NamedRange1 +1,first cell of NamedRange2+1,2,false)
next
Each time through the loop would take the next cell in the named ranges, but I don't know how to make this happen. Any suggestions on how to make this UDF work with named ranges? Thanks in advance!!