/ / Trova un testo o un numero da un elenco in un altro foglio ed evidenzia ogni istanza: vba, excel-vba, excel

Trova un testo o un numero da un elenco in un altro foglio ed evidenzia ogni istanza: vba, excel-vba, excel

Ho 2 fogli con più di 50.000 punti dati(Foglio 1), e una lista con numeri e testi alfanumerici più di 30K (Foglio 2, Colonna A). Voglio un codice macro per cercare ogni cella dal foglio 2 nel foglio 1 e cambiare il colore di sfondo di ogni istanza. Esempio: Ricerca: ABC123, dovrebbe trovare celle come "Stack_OverflowaBC123"## @@".

Ho trovato un codice, ma finora non soddisfano il mio requisito:

Sub HighlightListed()
Dim strConcatList As String
Dim cell As Range
For Each cell In Sheets("List").Range("A1:A30")
strConcatList = strConcatList & cell.Value & "|"
Next cell

For Each cell In Intersect(Sheets("Data").Range("A:A"), Sheets("Data").UsedRange) "I assume my problem is here somewhere, its only highlights exact results.
"If i am looking for "ABC123" it should also highlight cell like "PQRABC123" or ""XYZ_ABC123"
If InStr(strConcatList, cell.Value) > 0 Then
cell.Interior.Color = RGB(255, 0, 0)
End If
Next cell
End Sub

Sono abbastanza nuovo per VBA, quindi una spiegazione dettagliata sarà molto utile

risposte:

0 per risposta № 1

Prova questo:

Sub HighlightListed()
Dim searches As New Collection
Dim search As Variant
Dim cell As Range
For Each cell In Sheets("List").Range("A1:A30")
If cell.Value <> "" Then
searches.Add cell.Value
End If
Next cell
For Each cell In Intersect(Sheets("Data").Range("A:A"), Sheets("Data").UsedRange)
For Each search In searches
If InStr(0, cell.Value, Cstr(search), 1) > 0 Then
cell.Interior.Color = RGB(255, 0, 0)
Exit For
End If
Next search
Next cell
End Sub