Vai al contenuto principale

Come visualizzare e restituire il colore di sfondo insieme al valore di ricerca in Excel?

Supponendo che tu abbia una tabella come mostrato nell'immagine sottostante. Ora vuoi controllare se un valore specificato è nella colonna A e quindi restituire il valore corrispondente insieme al colore di sfondo nella colonna C. Come ottenerlo? Il metodo nell'articolo può aiutarti a risolvere il problema.

Vlookup e restituisce il colore di sfondo con il valore di ricerca tramite la funzione definita dall'utente


Vlookup e restituisce il colore di sfondo con il valore di ricerca tramite la funzione definita dall'utente

Si prega di fare quanto segue per cercare un valore e restituire il valore corrispondente insieme al colore di sfondo in Excel.

1. Nel foglio di lavoro contiene il valore che si desidera visualizzare, fare clic con il pulsante destro del mouse sulla scheda del foglio e selezionare Visualizza codice dal menu contestuale. Vedi screenshot:

2. In apertura Microsoft Visual Basic, Applications Edition finestra, copia sotto il codice VBA nella finestra del codice.

Codice VBA 1: Vlookup e restituisce il colore di sfondo con il valore di ricerca

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. Quindi fare clic inserire > Modulie copia il codice VBA 2 di seguito nella finestra del modulo.

Codice VBA 2: Vlookup e restituisce il colore di sfondo con il valore di ricerca

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. Dopo aver inserito i due codici, fare clic su Strumenti > Riferimenti. Quindi controlla il file Runtime di Microsoft Script casella nella Riferimenti - VBAProject la finestra di dialogo. Vedi screenshot:

5. premi il altro + Q tasti per uscire da Microsoft Visual Basic, Applications Edition finestra e torna al foglio di lavoro.

6. Selezionare una cella vuota adiacente al valore di ricerca e quindi immettere la formula =LookupKeepColor(E2,$A$1:$C$8,3) nella barra della formula, quindi premere il tasto Invio.

Note:: Nella formula, E2 contiene il valore che cercherete, $ A $ 1: $ C $ 8 è l'intervallo della tabella e il numero 3 significa che il valore corrispondente che restituirai si trova nella terza colonna della tabella. Si prega di cambiarli come necessario.

7. Continuare a selezionare la prima cella del risultato e trascinare la maniglia di riempimento verso il basso per ottenere tutti i risultati insieme al colore di sfondo. Vedi screenshot.


Articoli correlati:

I migliori strumenti per la produttività in ufficio

🤖 Assistente AI di Kutools: Rivoluziona l'analisi dei dati basandosi su: Esecuzione intelligente   |  Genera codice  |  Crea formule personalizzate  |  Analizzare i dati e generare grafici  |  Richiama le funzioni di Kutools...
Funzioni popolari: Trova, evidenzia o identifica i duplicati   |  Elimina righe vuote   |  Combina colonne o celle senza perdere dati   |   Round senza formula ...
Super ricerca: VLookup a criteri multipli    VLookup a valori multipli  |   VLookup su più fogli   |   Ricerca fuzzy ....
Elenco a discesa avanzato: Crea rapidamente un elenco a discesa   |  Elenco a discesa dipendente   |  Elenco a discesa a selezione multipla ....
Gestore di colonna: Aggiungi un numero specifico di colonne  |  Sposta colonne  |  Attiva/disattiva lo stato di visibilità delle colonne nascoste  |  Confronta intervalli e colonne ...
Funzionalità in primo piano: Messa a fuoco della griglia   |  Vista di progettazione   |   Grande barra delle formule    Gestore di cartelle di lavoro e fogli   |  Resource Library (Testo automatico)   |  Date picker   |  Combina fogli di lavoro   |  Crittografa/decrittografa le celle    Invia e-mail per elenco   |  Super filtro   |   Filtro speciale (filtro grassetto/corsivo/barrato...) ...
I 15 migliori set di strumenti12 Testo Strumenti (aggiungi testo, Rimuovi personaggi, ...)   |   50+ Grafico Tipi (Diagramma di Gantt, ...)   |   40+ Pratico Formule (Calcola l'età in base al compleanno, ...)   |   19 Inserimento Strumenti (Inserisci il codice QR, Inserisci immagine dal percorso, ...)   |   12 Conversione Strumenti (Numeri in parole, Conversione di valuta, ...)   |   7 Unisci e dividi Strumenti (Combina righe avanzate, Celle divise, ...)   |   ... e altro ancora

Potenzia le tue competenze di Excel con Kutools per Excel e sperimenta l'efficienza come mai prima d'ora. Kutools per Excel offre oltre 300 funzionalità avanzate per aumentare la produttività e risparmiare tempo.  Fai clic qui per ottenere la funzionalità di cui hai più bisogno...

Descrizione


Office Tab porta l'interfaccia a schede in Office e semplifica notevolmente il tuo lavoro

  • Abilita la modifica e la lettura a schede in Word, Excel, PowerPoint, Publisher, Access, Visio e Project.
  • Apri e crea più documenti in nuove schede della stessa finestra, piuttosto che in nuove finestre.
  • Aumenta la produttività del 50% e riduce ogni giorno centinaia di clic del mouse!
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