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
Paul*
.
?
*
H|l
C/O Timmy & Assoc
c/o
H\o
(
]
Smith!!
Paul/II
465 Estate of
Bob-Robert

<tbody>
</tbody>
 

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.

Drrellik

Well-known Member
Joined
Apr 29, 2013
Messages
773
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,549
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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]
 

pdiddy9590

New Member
Joined
Mar 12, 2018
Messages
5
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?
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,549
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows

ADVERTISEMENT

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]
 

Forum statistics

Threads
1,147,735
Messages
5,742,869
Members
423,760
Latest member
photogfrog

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
Top