substring (alternate) coloring

acol

New Member
Joined
Jan 21, 2021
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi All
I have a lot of cells, containing multiple substrings.
To make it more readable I would want some VBA code that:
* for all selected cells
* based on a given delimiter (mostly "; ")
* colors the strings alternately using 2 colors
(eg. string1 (+delimiter)(blue), string2 (+delimiter)(red), string3 (+delimiter)(blue), string4 (+delimiter)(red), etc...

I think this might be quite straight forward, but I lack the knowledge to figure it quite out.
Can anyone help me out?
Thank you
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
See if this is what you want.

VBA Code:
Sub ColourSubstrings()
  Dim itm As Variant
  Dim c As Range
  Dim k As Long, clr As Long
  
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    clr = vbBlue
    k = 1
    For Each itm In Split(c.Value, ";")
      c.Characters(k, Len(itm) + 1).Font.Color = clr
      clr = IIf(clr = vbBlue, vbRed, vbBlue)
      k = k + Len(itm) + 1
    Next itm
  Next c
End Sub

Here is my original data

acol.xlsm
A
1data 1;data 2;other data 3;and even longer data 4
2some text;some othrt text
3
Sheet1 (3)


.. and after the code ..

1701861656423.png


Is that what you were after?
 
Upvote 1
Solution
See if this is what you want.

VBA Code:
Sub ColourSubstrings()
  Dim itm As Variant
  Dim c As Range
  Dim k As Long, clr As Long
 
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    clr = vbBlue
    k = 1
    For Each itm In Split(c.Value, ";")
      c.Characters(k, Len(itm) + 1).Font.Color = clr
      clr = IIf(clr = vbBlue, vbRed, vbBlue)
      k = k + Len(itm) + 1
    Next itm
  Next c
End Sub

Here is my original data

acol.xlsm
A
1data 1;data 2;other data 3;and even longer data 4
2some text;some othrt text
3
Sheet1 (3)


.. and after the code ..

View attachment 103034

Is that what you were after?
Indeed, This is it (completely)!
All of blessings on this Saint-Nicholas day!
Thank you
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,129
Members
449,097
Latest member
mlckr

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