VBA to highlight certain cells with specific data

pscofe

New Member
Joined
Jan 24, 2020
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi, i'm looking for VBA code to highlight certain cells. My table below, I would like to highlight anything in local_acc_no that is "Paul, Matt, Antonia, Sebastian) and I want to highlight anything in currency that is NOT EUR, GBP or USD.
This table will change daily so some days will be less rows or more. Thanks

local_acc_noclosing_balancecurrencyclosing_balance_eurstmt_date
Paul
-740,000.00​
GBP
-873,266.60​
02/07/2020​
Jack
-462,227.10​
NZD
-270,215.65​
02/07/2020​
Chris
-346,275.07​
GBP
-408,635.75​
02/07/2020​
Matt
-286,560.49​
EUR
-286,560.49​
02/07/2020​
Marianne
-16,549.07​
EUR
-16,549.07​
02/07/2020​
Antonia
-7,038.68​
GBP
-8,306.28​
02/07/2020​
Sophia
-4.00​
ISK
-0.03​
02/07/2020​
Sebastian
-0.02​
EUR
-0.02​
02/07/2020​
Suzanne
-0.01​
USD
-0.01​
02/07/2020​
 
Hi, pscofe
Try this:
VBA Code:
Sub a1123944a()
'https://www.mrexcel.com/board/threads/vba-to-highlight-certain-cells-with-specific-data.1123944/
Dim n As Long
Dim va, ary
Dim d As Object

Application.ScreenUpdating = False
    ary = Array("Paul", "Matt", "Antonia", "Sebastian")
    n = Range("A" & Rows.Count).End(xlUp).Row
    ActiveSheet.AutoFilterMode = False

Call to_Color(ary, Range("A1:A" & n))

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
    va = Range("C2:C" & n)
    For Each x In va
        If x <> "EUR" And x <> "GBP" And x <> "USD" Then d(x) = Empty
    Next

Call to_Color(d.keys, Range("C1:C" & n))
Application.ScreenUpdating = True
End Sub

Function to_Color(ByVal z As Variant, c As Range)
With c
    .Interior.Color = xlNone
    .AutoFilter Field:=1, Criteria1:=z, Operator:=xlFilterValues
    On Error Resume Next
    .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Interior.Color = vbYellow
    On Error GoTo 0
    .AutoFilter
End With
End Function

Example:
Book1
ABCDE
1local_acc_noclosing_balancecurrencyclosing_balance_eurstmt_date
2Paul-740,000.00?GBP-873,266.60?02/07/2020?
3Jack-462,227.10?NZD-270,215.65?02/07/2020?
4Chris-346,275.07?GBP-408,635.75?02/07/2020?
5Matt-286,560.49?EUR-286,560.49?02/07/2020?
6Marianne-16,549.07?EUR-16,549.07?02/07/2020?
7Antonia-7,038.68?GBP-8,306.28?02/07/2020?
8Sophia-4.00?ISK-0.03?02/07/2020?
9Sebastian-0.02?EUR-0.02?02/07/2020?
10Suzanne-0.01?USD-0.01?02/07/2020
Sheet3
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Akuni, that looks fantastic! My sheet will always be called 'Overdrafts' how can I point that code to run on the correct sheet? Thanks
 
Upvote 0
Hi Akuni, that looks fantastic! My sheet will always be called 'Overdrafts' how can I point that code to run on the correct sheet? Thanks
Try this one:
VBA Code:
Sub a1123944a()
'https://www.mrexcel.com/board/threads/vba-to-highlight-certain-cells-with-specific-data.1123944/
Dim n As Long
Dim va, ary
Dim d As Object

Application.ScreenUpdating = False

With Sheets("Overdrafts")
    
    ary = Array("Paul", "Matt", "Antonia", "Sebastian")
    n = .Range("A" & .Rows.Count).End(xlUp).Row
    ActiveSheet.AutoFilterMode = False

    Call to_Color(ary, .Range("A1:A" & n))
    
    Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
        va = .Range("C2:C" & n)
        For Each x In va
            If x <> "EUR" And x <> "GBP" And x <> "USD" Then d(x) = Empty
        Next
    
    Call to_Color(d.keys, .Range("C1:C" & n))

End With

Application.ScreenUpdating = True
End Sub

Function to_Color(ByVal z As Variant, c As Range)
With c
    .Interior.Color = xlNone
    .AutoFilter Field:=1, Criteria1:=z, Operator:=xlFilterValues
    On Error Resume Next
    .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Interior.Color = vbYellow
    On Error GoTo 0
    .AutoFilter
End With
End Function
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top