Highlight Cell If

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good morning

Is there a way to highlight a cell if there are more than 2 letters following the number in a string.

Before

101po
A 1002pore
1003ro Next
1004pos End
A 1006po
1002pow

<tbody>
</tbody>


After

1001po
A 1002pore
1003ro Next
1004pos End
A 1006po
1002pow

<tbody>
</tbody>
 
I've uploaded additional data to the ws. There are strings mixed in with no numerals in between which are highlighted, Is there a way to not highlight these.:)

example..

A PO
B PO
A Purchase
Give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub HighlightIfNotTwoLetters()
  Dim Dn As Range
  For Each Dn In Range("A1", Cells(Rows.Count, "A").End(xlUp))
    Dn.Font.Bold = Not " " & Dn & " " Like "*##[A-Za-z][A-Za-z] *" And Not Dn & " " Like "*## *" And Dn Like "*#*"
  Next
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Trying to understand what the code is doing.
See if this helps any...

Range("A1", Cells(Rows.Count, "A").End(xlUp))
----------------------------------------------------------
Finds last data cell in Column A


Dn.Font.Bold
-------------------
Dn is the cell being iterated in the above range and we are going to assign the expression below to its Font.Bold property


For Each Dn In Range("A1", Cells(Rows.Count, "A").End(xlUp))
-------------------------------------------------------------------------
Iterates each cell in the range A1 down to the last cell in Column A


Not " " & Dn & " " Like "*##[A-Za-z][A-Za-z] *" And Not Dn & " " Like "*## *" And Dn Like "*#*"
-----------------------------------------------------------------------------------------------------------------
Contains three logical expressions (highlighted in red, blue and green) that all must be true (hence, the And operators) in order to return True to the above Font.Bold property of the cell currently being iterated in the For..Next loop. Like is a logical operator in VB and what it does is compare the text to the left of it to a text pattern made up of wildcards (? and *) and character groups (what is inside square brackets).


Not " " & Dn & " " Like "*##[A-Za-z][A-Za-z] *"
--------------------------------------------------------
Here I concatenate spaces in front of (which is actually not required for how I code this logical expression) and behind (this is required to handle the case when nothing number/letters come at the end of the text) the value in the currently being iterated cell and then test this against the pattern which looks for the negative (important that you remember that) of the pattern formed by zero or more characters followed by a mandatory two digits followed by two mandatory upper or lower case letters followed by a mandatory space followed by zero or more characters. This will return True for any text that does NOT contain two digits followed by two letters somewhere in the text.


Not Dn & " " Like "*## *"
--------------------------------
Here I concatenate as space onto the back of the currently being iterated cell (for the same reason as in the above description) and then test it against the pattern which looks for the negative (again, important that you remember this) of the pattern formed by zero or more characters followed by two mandatory digits followed by a mandatory space followed by zero or more characters. This will return True for text contains two digits followed by any thing other than a space.


Dn Like "*#*"
----------------------
This returns True if the text in the cell being iterated contains at least one digit.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,690
Members
449,117
Latest member
Aaagu

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