Find special characters in a cell and highlight with vba

pdiddy9590

New Member
Joined
Mar 12, 2018
Messages
5
Is there an easier way of highlighting cells that contain a special character (!~`@#$%^&*()_+-={}|:"<>?[]\;',./) instead of listing everyone separate like below? There can be letters and numbers along with the special characters.

If ActiveCell.Offset(0, 4).Value Like "*.*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*~*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*`*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*!*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*@*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*#*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*$*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*%*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*^*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*&*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
ElseIf ActiveCell.Offset(0, 4).Value Like "*_*" Then
ActiveCell.Offset(0, 4).Interior.ColorIndex = 6
End If


example
[TABLE="width: 182"]
<tbody>[TR]
[TD]Paul*
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[/TR]
[TR]
[TD]?
[/TD]
[/TR]
[TR]
[TD]*
[/TD]
[/TR]
[TR]
[TD]H|l
[/TD]
[/TR]
[TR]
[TD]C/O Timmy & Assoc
[/TD]
[/TR]
[TR]
[TD]c/o
[/TD]
[/TR]
[TR]
[TD]H\o
[/TD]
[/TR]
[TR]
[TD](
[/TD]
[/TR]
[TR]
[TD]]
[/TD]
[/TR]
[TR]
[TD]Smith!!
[/TD]
[/TR]
[TR]
[TD]Paul/II
[/TD]
[/TR]
[TR]
[TD]465 Estate of
[/TD]
[/TR]
[TR]
[TD]Bob-Robert
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Pdiddy,

This is a piece of VBA I took out of something a pain snakingly put together with help from Google and this forum two years ago so I could highlight any row out to F column if it found two words, you may be able to use this to help you get started and hopefully someone else will be able to help more.

Signed...Still learning.:)

Code:
Dim c As Range
For Each c In ActiveSheet.Range("F2:F" & Range("F" & Rows.Count).End(xlUp).Row)
   
    If InStr(c, "R6002") > 0 Then
       c.Offset(, -5).Resize(1, 7).Interior.ColorIndex = 6
    End If
   
Next c
Dim D As Range
For Each D In ActiveSheet.Range("G2:G" & Range("G" & Rows.Count).End(xlUp).Row)
   
    If InStr(D, "HOT GUN") > 0 Then
       D.Offset(, -6).Resize(1, 7).Interior.ColorIndex = 6
    End If
   
Next D
 
Upvote 0
Is there an easier way of highlighting cells that contain a special character (!~`@#$%^&*()_+-={}|:"<>?[]\;',./) instead of listing everyone separate like below?
If by "special character" you mean any non-alphanumeric character, then this single line of code is all you need...
Code:
[table="width: 500"]
[tr]
	[td]If ActiveCell.Offset(0, 4).Value Like "*[!A-Za-z0-9]*" Then ActiveCell.Offset(0, 4).Interior.ColorIndex = 6[/td]
[/tr]
[/table]
 
Upvote 0
Rick,
Thanks for your reply. I was using that code but it highlights the cell if it contains Estate of, Bob III and Bob Jr. Do you know why it is highlighting those?
 
Upvote 0
Rick,
Thanks for your reply. I was using that code but it highlights the cell if it contains Estate of, Bob III and Bob Jr. Do you know why it is highlighting those?
I forgot to include a space character inside the square brackets...
Code:
[table="width: 500"]
[tr]
	[td]If ActiveCell.Offset(0, 4).Value Like "*[!A-Za-z0-9 ]*" Then ActiveCell.Offset(0, 4).Interior.ColorIndex = 6[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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