I have a UDF which is essentially a vlookup function except that it also returns any cell comments in addition to the cell values (see code below). I got this code from this forum, but I don't remember who to give the credit to (sorry).
Can anyone think of a way to modify this function to also return the cell formatting, specifically the color of the cell? I thought I was done with this project two months ago, but a bug just emerged that could quickly be squashed if I could just modify this function. Thanks for any ideas!
Can anyone think of a way to modify this function to also return the cell formatting, specifically the color of the cell? I thought I was done with this project two months ago, but a bug just emerged that could quickly be squashed if I could just modify this function. Thanks for any ideas!
Code:
Option Explicit
Function VlookupComment(LookupValue As Variant, LookupTable As Range, ColumnNumber As Long, MatchType As Boolean) As Variant
'This is essentially a VLOOKUP function except that it returns cell contents AND cell Comments
'Formula to be typed into cell of spreadsheet is identical to a regular VLOOKUP: =VlookupComment(lookup_value,table_array,col_index_num,false)
Application.Volatile True
Dim res As Variant
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
Else
.Comment.Delete
End If
If myLookupCell.Comment Is Nothing Then
Else
.AddComment Text:=myLookupCell.Comment.Text
End If
End With
End If
End Function