How to change partial font color in a cell which is found by another cell?

Rajib

New Member
Joined
Mar 11, 2022
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
  2. Web
I want to change the partial font color (red) found in B1 match by column A.
 

Attachments

  • Screenshot_1.jpg
    Screenshot_1.jpg
    38.2 KB · Views: 14
@Peter_SSs

may you mod this line

VBA Code:
  For Each c In Range("B1:C4")
I would to be dynamic . is there any way if the range increases without I have to change the range when increase data every time?
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
@Peter_SSs

may you mod this line

VBA Code:
  For Each c In Range("B1:C4")
I would to be dynamic . is there any way if the range increases without I have to change the range when increase data every time?
Perhaps you you start your own thread rather than hijack this current one. You can put a link to this thread in your new one if you want.
 
Upvote 0
I was looking for a solution to this for a while now and found out it worked perfectly. Almost! I've encountered a small thing I can't fix.
As you can see from my code, I've used this to mark some words Bold in the Targeted cell.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("O:O")) Is Nothing Then
    Dim RX As Object, M As Object
    Dim a As Variant
    Dim i As Long
    
    Target.Font.Bold = False
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.IgnoreCase = True
    RX.MultiLine = True
    a = Keywords.Range("A1", Keywords.Range("A" & Rows.Count).End(xlUp)).Value
    For Each c In Target
    With c
        For i = 1 To UBound(a)
          RX.Pattern = a(i, 1)
          For Each M In RX.Execute(.Value)
            .Characters(M.FirstIndex + 1, Len(M)).Font.Bold = True
          Next M
        Next i
      End With
    Next c
End If
End Sub

SS 2022-03-19 213502.png
In my list there are some words like: "Battlecry", "Charge", "Taunt", "Legendary", ... And those work fine.
But with the options: "Spell Damage", "Spell Damage +1", "Spell Damage +2", ... and "Nature Spell Damage +3"; something is going not as wanted.
In all these instances the code can't find the full string, and skips it.
I've got no clue why only "Spell Damage" works and not the other variants.

I would appreciate any help I can get on this.
 
Upvote 0
I would appreciate any help I can get on this.
Welcome to the MrExcel board!

Can you use XL2BB to give us the following so we can assess the keyword list, what is in each cell, and not have to manually type out the samples?
a) The sample data, and
b) The keywords list from column A

Is the data in a formal Excel table (ListObject)?
 
Upvote 0
As requested below the sample data with XL2BB.
All data is in Excel Tables.

And thx for welcoming me :)

Partial bold.xlsm
ABCD
1InfoKeyword
2Gain 1 Mana Crystals this turn only.Battlecry
3Deal 1 damage.Charge
4Battlecry: Spend all your Mana. Gain +1/+1 for each mana spent.Choose One
5Choose One - Summon a Jade Golem; or Shuffle 3 copies of this card into your deck.Deathrattle
6Quest: Summon 5 minions with 5 or more Attack. Reward: BarnabusDiscover
7Choose One - Deal 2 damage; or Summon two 1/1 Saplings.Jade Golem
8Choose One - Discover a minion; or Discover a spell.Nature Spell Damage +3
9Deal damage equal to your hero's Attack to a minion.Spell Damage
10Taunt. Choose One - +1 Attack; or +1 Health.Spell Damage +2
11Battlecry: Gain an empty Mana Crystal. Deathrattle: Lose a Mana Crystal.Spell Damage +3
12Battlecry: Destroy all damaged minions.
13Battlecry: Draw 3 cards. Put any minions you drew directly into the battlefield.
14Nature Spell Damage +3
15Spell Damage +1
16
Cardlist
 
Upvote 0
Thanks for the samples. Sheets/ranges etc seem to have changed though so this is a bit of a guess.

I have assumed that
- the keyword list is in a table called "Table2" in a column called "Keyword" on a worksheet called "Keywords"
- the data to be bolded or not is in a table called "Table1" in a column called "Info"

One of the issues you are facing is that the "+" symbol has a special meaning when using regular expressions so that needs to be treated in a special way.

Based on the above, try this in the sheet module that contains Table1

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long
  Dim Changed As Range, c As Range
  
  Set Changed = Intersect(Target, Range("Table1[Info]"))
  If Not Changed Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.IgnoreCase = True
    RX.MultiLine = True
    Changed.Font.Bold = False
    a = Sheets("Keywords").Range("Table2[Keyword]").Value
    For Each c In Changed
      With c
        For i = 1 To UBound(a)
          RX.Pattern = Replace(a(i, 1), "+", "\+")
          For Each M In RX.Execute(.Value)
            .Characters(M.FirstIndex + 1, Len(M)).Font.Bold = True
          Next M
        Next i
      End With
    Next c
  End If
End Sub
 
Upvote 0
I didn't know about + being special, haha
This is my first time adapting and using regular expressions. Before I found this post I had no Idea it existed.

Now it works as a charm without any problems.

@Peter_SSs Thank you very much for your help.
@Rajib Thanks for reminding me I that I still needed to solve this on my own sheets. Been working on this for a very long time.
 
Upvote 0
You're welcome. Glad it is resolved. Thanks for letting us know. :)

One thing that I forgot to mention before. This is a check for strings, not for words.
So if you enter
I recharged the battery
you will get
I recharged the battery

or
Spell Damage +25
Spell Damage +25
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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