跳到主要內容

如何在Excel中進行vlookup並返回背景色以及查找值?

假設您有一個表格,如下所示。 現在,您要檢查指定的值是否在A列中,然後在C列中返回相應的值以及背景色。如何實現? 本文中的方法可以幫助您解決問題。

Vlookup並通過用戶定義的函數返回具有查找值的背景色


Vlookup並通過用戶定義的函數返回具有查找值的背景色

請執行以下操作以在Excel中查找值並返回其對應的值以及背景色。

1.在工作表中包含您要vlookup的值,右鍵單擊工作表選項卡,然後選擇 查看代碼 從上下文菜單中。 看截圖:

2.在開幕 Microsoft Visual Basic for Applications 窗口,請將下面的VBA代碼複製到“代碼”窗口中。

VBA代碼1:Vlookup並返回具有查找值的背景色

Sub Worksheet_Change(ByVal Target As Range)
    Dim I As Long
    Dim xKeys As Long
    Dim xDicStr As String
    On Error Resume Next
    Application.ScreenUpdating = False
    xKeys = UBound(xDic.Keys)
    If xKeys >= 0 Then
        For I = 0 To UBound(xDic.Keys)
            xDicStr = xDic.Items(I)
            If xDicStr <> "" Then
                Range(xDic.Keys(I)).Interior.Color = _
                Range(xDic.Items(I)).Interior.Color
            Else
                Range(xDic.Keys(I)).Interior.Color = xlNone
            End If
        Next
        Set xDic = Nothing
    End If
    Application.ScreenUpdating = True
End Sub

3。 然後點擊 插入 > 模塊,並將以下 VBA 代碼 2 複製到模塊窗口中。

VBA代碼2:Vlookup並返回具有查找值的背景色

Public xDic As New Dictionary
Function LookupKeepColor (ByRef FndValue, ByRef LookupRng As Range, ByRef xCol As Long)
    Dim xFindCell As Range
    On Error Resume Next
    Set xFindCell = LookupRng.Find(FndValue, , xlValues, xlWhole)
    If xFindCell Is Nothing Then
        LookupKeepColor = ""
        xDic.Add Application.Caller.Address, ""
    Else
        LookupKeepColor = xFindCell.Offset(0, xCol - 1).Value
        xDic.Add Application.Caller.Address, xFindCell.Offset(0, xCol - 1).Address
    End If
End Function

4.插入兩個代碼後,然後單擊 工具 > 參考。 然後檢查 Microsoft腳本運行時 盒子裡 參考– VBAProject 對話框。 看截圖:

5。 按 其他 + Q 退出鍵 Microsoft Visual Basic for Applications 窗口,然後返回工作表。

6. 選擇與查找值相鄰的空白單元格,然後輸入公式 =LookupKeepColor(E2,$A$1:$C$8,3) 進入公式欄,然後按Enter鍵。

備註: 在公式, E2 包含您要查找的值, $ A $ 1:$ C $ 8 是表格範圍和數字 3 表示您將返回的相應值位於表的第三列。 請根據需要更改它們。

7. 繼續選擇第一個結果單元格,然後向下拖動填充手柄以獲取所有結果及其背景顏色。 請參閱屏幕截圖。


相關文章:

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (34)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
This code is working fine , except in the cells that the formula is inputed in bring up 0 when the cell it is looking up is blank , my question is how do i make it ignore blank cells and prevent the cell the formula is in from inputting a 0 , is there some wheres in the code to enter an =IFERROR function maybe ?
This comment was minimized by the moderator on the site
Hi Kyle,

I tested this code and it does not bring up 0 when the cell it looks for is blank.
Perhaps you could include the formula in the IF function, as shown below, to prevent returning a result of 0.
=IF(B2="","",LookupKeepColor(E2,$A$1:$C$8,3))
This comment was minimized by the moderator on the site
Hi, Is this code working for office 2016 and later versions ?
This comment was minimized by the moderator on the site
no its not returning color.
This comment was minimized by the moderator on the site
I use times and dates from excel reports to create timesheets for our employees. If the specified date, for example, 2020/08/11 matches the date on the next tabs array (which contains many cells with the same date but different times) I want it to pull only the cell filled in orange which will be stated as 2020/08/11 7:45. Is this possible?
This comment was minimized by the moderator on the site
How I do
This comment was minimized by the moderator on the site
Hello, i have a sheet with 10,948 rows, its taking some time to pull the information with colors, still waiting. Is this normal, or there is something wrong?
This comment was minimized by the moderator on the site
This was AWESOME! followed the steps and it works beautifully! Thank you!
This comment was minimized by the moderator on the site
I have many records, it takes too long to process, and the code keeps on running even after completion. Please help
This comment was minimized by the moderator on the site
I have used this in Excel 2016 and only the data is transferred from Source to Target...….color is not transferred. Thoughts on what issue might be: Is it incompatibility with Excel 2016? Thanks. MT
This comment was minimized by the moderator on the site
i am getting the required cell color but i also need the lookup value as it is returning integer instead of string
This comment was minimized by the moderator on the site
Hi, Can i apply vlookup on color cells with no data in them
This comment was minimized by the moderator on the site
this works fine in office 2010, but not the 2013 version. Is there an update to the macro?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations