Skip to main content

如何在 Excel 中使用 vlookup 查找並返回查找值及其背景色?

Author: Siluvia Last Modified: 2025-05-12

假設您有如下截圖所示的表格。現在,您想檢查指定的值是否在 A 列中,然後返回 C 列中對應值及其背景色。該怎麼實現呢?本文中的方法可以幫助您解決這個問題。

A screenshot of a sample Excel table to perform VLOOKUP with background color return

通過用戶自定義函數進行 Vlookup 並返回查找值的背景色


通過用戶自定義函數進行 Vlookup 並返回查找值的背景色

請按照以下步驟在 Excel 中查找值並返回其對應值及背景色。

1. 在包含要 vlookup 值的工作表中,右鍵單擊工作表標籤,然後從上下文菜單中選擇 檢視代碼。參見截圖:

A screenshot showing the right-click context menu on an Excel sheet tab with 'View Code' option highlighted

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. 插入兩個代碼後,點擊 工具 > 參考資料。然後在 參考資料 – VBAProject 對話框中勾選 Microsoft Script Runtime 選項。參見截圖:

A screenshot of the References dialog box in VBA with 'Microsoft Script Runtime' option checked

5. 按下 Alt + Q 鍵退出 Microsoft Visual Basic for Applications 窗口並返回工作表。

6. 選擇查找值旁邊的一個空白單元格,然後在 編輯欄(F) 中輸入公式 =LookupKeepColor(E2,$A$1:$C$8,3) ,然後按 Enter 鍵。

A screenshot of the LookupKeepColor formula entered in Excel with the color retained in the result

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

7. 繼續選擇第一個結果單元格,並向下拖動填充柄以獲取所有結果及其背景色。參見截圖。

A screenshot showing the final VLOOKUP results with background colors retained


相關文章:

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!