Highlight cell based on other cell criteria

Chaozfate

Board Regular
Joined
Mar 15, 2017
Messages
71
Hi, I know this can be done in conditional formatting, but I would like to solve this with VBA. The cells will be continuously expanding by copy and paste the existing cell, the conditional formatting formula will follow to increase an cause the excel to overload.

I would like something as following:

Highlight cell A2 in blue if cell B2 detect a criteria with wild card.
Says
A2 will be highlighted if B2 is "*fund in*"
A6 will be highlighted if B6 is "*fund in*"

Would be appreciate if anyone could help on this.

Thanks and regards,
CCU
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Apply this code for first time run (with text string in column B has exists):
Put it in workbook module (Alt-F11, click on "ThisWorkbook", paste below code):
VBA Code:
Option Explicit

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim lr&, f, ad As String
lr = Sh.Cells(Rows.Count, "B").End(xlUp).Row
Sh.Range("A2:A100000").Interior.Color = xlNone
Set f = Sh.Range("B2:B" & lr).Find("fund in", LookAt:=xlPart, searchorder:=xlByRows)
If Not f Is Nothing Then
    f.Offset(0, -1).Interior.Color = vbYellow
    ad = f.Address
    Do
        Set f = Sh.Range("B2:B" & lr).FindNext(f)
        If Not f Is Nothing Then f.Offset(0, -1).Interior.Color = vbYellow
    Loop Until f.Address = ad
End If
Set f = Nothing
End Sub

For next run, below code fires any change in column B :
Put it in specific worksheet module (Right click on tab name, View code, paste below code)
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lr&, f, ad As String
If Intersect(Target, Columns(2)) Is Nothing Then Exit Sub
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("A2:A100000").Interior.Color = xlNone
Set f = Range("B2:B" & lr).Find("fund in", LookAt:=xlPart, searchorder:=xlByRows)
If Not f Is Nothing Then
    f.Offset(0, -1).Interior.Color = vbYellow
    ad = f.Address
    Do
        Set f = Range("B2:B" & lr).FindNext(f)
        If Not f Is Nothing Then f.Offset(0, -1).Interior.Color = vbGreen
    Loop Until f.Address = ad
End If
Set f = Nothing
End Sub

If you do not want the first code (sheet activate), use the second, but for 1st run, F2 (activate) any cell in column B then Enter
 
Upvote 0
thank you so much for the drafted code, however in column A, there is other cells with different color, by applying the above code, the vba overwrite the other cells color too.

wonder if it is possible to eliminate that.

Apologies for the inconvenience caused.
 
Upvote 0
If you understand how the code work, its easy to twist:
In fifth lines:
VBA Code:
Range("A2:A100000").Interior.Color = xlNone
just remove it.
 
Upvote 0
Assuming that it is the wildcard (*) that you are interested in, not just "fund in" and the 'wildcard rows' are to be coloured vbCyan and your other colour(s) are different then try this Worksheet_Change event code

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Application.ScreenUpdating = False
  With Range("A1", Range("B" & Rows.Count).End(xlUp))
    .AutoFilter Field:=1, Criteria1:=vbCyan, Operator:=xlFilterCellColor
    .Columns(1).SpecialCells(xlVisible).Interior.Color = xlNone
    .AutoFilter Field:=1
    .AutoFilter Field:=2, Criteria1:="=*~**"
    With .Columns(1)
      If .SpecialCells(xlVisible).Count > 1 Then .SpecialCells(xlVisible).Interior.Color = vbCyan
      .Cells(1).Interior.Color = xlNone
    End With
    .AutoFilter Field:=2
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,679
Messages
6,126,186
Members
449,296
Latest member
tinneytwin

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