The built-in VLOOKUP function in excel can find and return values only. But, it is not possible to copy comments along with values using this function. Using a user-defined function (UDF) can be a way of doing this. Follow the article to learn how to do that.
Download Practice Workbook
You can download the practice workbook from the download button below.
Steps to Copy Comments in Excel Using User-Defined Vlookup Function
Assume you want to look up a value along with the comments (if any) in the range B5:C9. The lookup value is given in cell E5. Now you want to use a VLOOKUP formula to return a value associated with the lookup value along with comments if it contains any. But as we have said, the VLOOKUP function is not fit for this job, i.e. copying the comments. So, we will create a UDF which works with the same mechanism as the VLOOKUP function does.
Follow the steps below to be able to do that.
📌 Step 1: Open a New Module to Insert VBA Code
- First, press ALT+F11 to open the VBA window. Then select Insert >> New Module to create a new module as shown in the following picture.
Read More: How to Copy Comments in Excel (2 Suitable Ways)
Similar Readings
- How to Extract Comments in Excel (3 Suitable Examples)
- [Fixed!] Comments in Excel Far Away from Cell (3 Possible Solutions)
- How to Add Comment in Excel (4 Handy Methods)
- How to Remove Comments in Excel (7 Quick Methods)
- Creating and Editing Excel Comments to Cells – [An Ultimate Guide]!
📌 Step 2: Write VBA Code to Create UDF
- After that, copy the following code using the copy button.
Function VLOOKUPCMT(lookup_value As Variant, table_array As Range, col_index_num As Long, range_lookup As Long) As Variant
Application.Volatile
Dim xReturn As Variant
Dim yCell As Range
xReturn = Application.Match(lookup_value, table_array.Columns(1), range_lookup)
If IsError(xReturn) Then
VLOOKUPCMT = "Not Found"
Else
Set yCell = table_array.Columns(col_index_num).Cells(1)(xReturn)
VLOOKUPCMT = yCell.Value
With Application.Caller
If Not .Comment Is Nothing Then
.Comment.Delete
End If
If Not yCell.Comment Is Nothing Then
.AddComment yCell.Comment.Text
End If
End With
End If
End Function
- Next, paste the copied code onto the blank module as shown in the picture below.
Read More: How to Reference Comments in Excel (3 Easy Methods)
📌 Step 3: Apply the User-Defined Function to Copy Comments
- Now, enter the lookup value (3) in cell E5.
- Finally, enter the following formula in cell F5. After that, you will get the desired result as follows. Here, the VLOOKUPCMT function works the same as the VLOOKUP function.
=VLOOKUPCMT(E5,B5:C9,2,FALSE)
VBA Code Explanation:
- Function VLOOKUPCMT(lookup_value As Variant, table_array As Range, col_index_num As Long, range_lookup As Long) As Variant
Here, the VLOOKUPCMT function will be the alternative to the VLOOKUP function in excel. It has the same arguments as the VLOOKUP function. - Application.Volatile
Forces a recalculation whenever a calculation occurs in any other cells. - Dim xReturn As Variant
Dim yCell As Range
Declaring necessary variables. - xReturn = Application.Match(lookup_value, table_array.Columns(1), range_lookup)
Assigning values to the xReturn variable. - If IsError(xReturn) Then
VLOOKUPCMT = “Not Found”
Returns Not Found in case of errors. - Set yCell = table_array.Columns(col_index_num).Cells(1)(xReturn)
Assigns a cell reference to the yCell variable. - VLOOKUPCMT = yCell.Value
The VLOOKUPCMT function returns the value of the cell referenced by the yCell variable. - With Application.Caller
If Not .Comment Is Nothing Then
.Comment.Delete
Delete any existing comments from the formula cell. - If Not yCell.Comment Is Nothing Then
.AddComment yCell.Comment.Text
Copy the comment from the cell referenced by the yCell variable to the cell containing the formula.
Things to Remember
You may need to recalculate every time you lookup for a new value. Otherwise, you may get erroneous results.
Conclusion
Now you know how to copy comments in excel using the VLOOKUP formula. Hopefully, this has solved your problem. Please use the comment section below for further queries or suggestions. Do visit our ExcelDemy blog to explore more about excel. Stay with us and keep learning.
Related Articles
- How to Extract Comments from Word Document into Excel
- Difference Between Threaded Comments and Notes in Excel
- How to Read Full Comment in Excel (6 Handy Methods)
- Populate Comment from Another Cell in Excel (and Vice Versa)
- How to Hide Comments in Excel (4 Quick Methods)
- Print Comments in Excel (4 Suitable Ways)
- How to Find Comments in Excel (4 Easy Methods)
Salam Shamim
Is it possible to add code to pull the picture from the comment?
Walaikum Assalam, KJ. Thanks for your query.
You can try the following code. The pictures will be copied to the next column.
Then, you can resize them and right-click to save them as pictures.
Alternatively, you can copy the worksheet or the workbook and save it as Web Page (*.htm, *.html).
Then, all of the pictures will be saved to a new folder (named after the file) in the same location as the saved file.
Hello. What is the code for transferring images and text from the comment field?
Hello. Thanks for the great help here. Little change here.
I wish that
comment field with image and text is transferred to another cell as a comment field with the same image and text. 🙂
Hello NILSEN,
Hope you are doing well. Here, I have inserted a comment to cell B3 and in this comment, we have some text along with an image. So, using a VBA code I will show the process of extracting the image and texts in different cells.
• Type the following code.
In the figure above, look at the highlighted portions which you may want to change.
Set Range = .Parent.Offset(0, 4)
will insert the image in a cell which is 4 columns right to the main cell B3 and.Parent.Offset(0, 3)
will insert the texts in a cell which is 3 columns right to the main cell B3.• Press F5.
Then, we transferred the texts and images from the comment into different cells.
Best Regards,
Tanjima Hossain
ExcelDemy
Hello. Thanks for the great help here. Little change here.
I wish that
comment field with image and text is transferred to another cell as a comment field with the same image and text. 🙂
Hello. Tries again.
Has comment field with image and text in cell b3 sheet 1
must be transferred
to sheet2 cell d3 as a comment field with the same image and text.
Hi need help code or formula here. Wishes.
Sheet1 (Search product) in cell B5.
If I write (Milk), I want the comment field with image and text from Sheet2 cell d5 to appear in Sheet1 cell C5.
Sheet2.
list
cell D5(milk) picture of milk in the comment field
cell D6 (bread) Picture of bread in the comment field
cell D7 (newspaper) Picture of a newspaper in the comment field
forgot to mention that the comment field in sheet1 Cell b5, that it retrieves the same size of the comment field that is made in Sheet2.
there is a formula i need in Sheet1 cell B4.
Today I have VLOOKUPCMT but it only retrieves text from the comment field.
Is there any other formula I can use to get a comment field with an image and text.
Hello Nilsen,
Sorry, this formula will not work for copying both a comment and a picture as a comment. You have to copy only the text strings if you want to paste them as comments. But if you want to copy the contents as cell value then try the previous code.
Stay in touch with ExcelDemy.
Thanks
Tanjima