Excel-VBA macro to format inconsistent text strings

Arturo333

New Member
Joined
Aug 15, 2018
Messages
4
It seems to be a huge challenge for Excel VBA to format inconsistent text strings and so I am looking for a solution to format the following table:

5j1FK.png


The table may contain hundreads of rows that have to be manually checked and so it is important to make it as readable as possible. Unfortunately, the column C contains a very long and messy text string that is inconsistent and cannot be changed since it is being imported from another system.


Therefore I would like to do the following:

  • make all the unimportant parts of the text string in column C light-grey highlight rows (cells in columns A:C)
  • if 'the_good_indicator' is found in column C highlight specific cells with certain string elements in them (e.g. phone number '00111222333')

The list of good indicator keywords might be quite long, but in this example it is just "the_good_indicator" and "00111222333" for background color and ";Names=", "PhoneNumber=", ";mobilePhoneNumber=", ";something_unimportant=YES". Denominators are <code>;</code> and <code>=</code>

The table should look like this:
ixzr7.png


I have done some research and found just partial answers, so...

The best I can get is table like this:

zHQgi.png


...which I get by using the code:
Code:
<code>Columns("C").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="the_good_indicator", _
    TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
    .Color = -16752384
    .TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ColorIndex = 43
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="00111222333", _
    TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
    .Color = -16752384
    .TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 13561798
    .TintAndShade = 0
End With</code>
I also got an expert suggestion to use INSTR or Regular Expressions together with .Characters property of the Range object to do the formatting. Unfortunately, I am lacking experience and knowledge to apply these methods at the moment...
I will appreciate any code improvement suggestions that could help reaching the formatting-goal at least partly.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
How do you define the 'unimportant' things in the strings?
 
Upvote 0
'Unimportant' is anything that should be out of the reader's focus. In this case: ";Names=", "PhoneNumber=", ";mobilePhoneNumber=", ";something_unimportant=YES"
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,993
Members
449,201
Latest member
Lunzwe73

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