Conditional Formating on Non-Letters and Non-Numbers

Jet35

New Member
Joined
Mar 15, 2016
Messages
10
First time poster, long time reader, thanks for all the past help and on this question...
I have a doc that several other employees use and one field requires users to input a Unique Name. The Unique Name can only use Letters and Numbers though, so I'd like to do conditional formatting when a user enters anything that is NOT a Letter or Number. Any tips?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
How about this?

Right click on the sheet you are working with. Click view code. Paste the following

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim t As Integer
For i = 1 To Len(Target)
    t = Asc(Mid(Target, i, 1))
    If Not ((t >= 48 And t <= 57) Or (t >= 65 And t <= 90) Or (t >= 97 And t <= 122)) Then
        With Target.Characters(i, 1)
            .Font.Bold = True
            .Font.Color = vbRed
        End With
    End If
Next i
End Sub
 
Upvote 0
You can use this as a Conditional Formatting formula:

=SUMPRODUCT(--ISERROR(SEARCH("~"&MID(J1&"aaaaaaaaaa",ROW(INDIRECT("1:10")),1),"abcdefghijklmnopqrstuvwxyz0123456789")))

It does assume the Unique Name is 10 characters or less.
 
Upvote 0
Eric W, that's a cool formula. Can you explain the purpose of the "~" in the Search function? Why is that have to be in there?
 
Upvote 0
Thanks for the kind words about my formula! I originally tried to create one using the same type of logic you used in your code, using CODE, but it got ugly real fast.

The SEARCH function allows wildcards, specifically "*" and "?". So if someone uses a * or ?, then the formula wouldn't catch it since they'd match anything. If you want to make SEARCH look for an actual * or ?, then you have to precede it with a ~. And fortunately, it doesn't hurt to put a tilde in front of any other letter.

One thought about your code: you may want to clear the formatting if they enter a valid user id, otherwise if they enter a bad one, then fix it, it won't show as fixed.
 
Last edited:
Upvote 0
Cool, great to know. I really like these formulas and this is a great solution.

Having said that, if you want to go the VBA route, I adjusted the code to account for some error handling.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        Dim t As Integer
        For i = 1 To Len(Target)
            t = Asc(Mid(Target, i, 1))
            If Not ((t >= 48 And t <= 57) Or (t >= 65 And t <= 90) Or (t >= 97 And t <= 122)) Then
                With Target.Characters(i, 1)
                    .Font.Bold = True
                    .Font.Color = vbRed
                End With
            End If
        Next i
    End If
End Sub
 
Upvote 0
You can use this as a Conditional Formatting formula:

=SUMPRODUCT(--ISERROR(SEARCH("~"&MID(J1&"aaaaaaaaaa",ROW(INDIRECT("1:10")),1),"abcdefghijklmnopqrstuvwxyz0123456789")))

It does assume the Unique Name is 10 characters or less.

Thanks Eric. The Unique Name can be up to 27 characters, but this doesn't seem to be working for me. Here's some more detail:
1) Highlighted the Unique Name column (I9:J29, it's a merged cell for ascetic reasons b/c of cells above them to make it easier for other people to read, but I can split if need be)
2) Conditional Formatting > New Rule > Use formula to determine which cells to format
3) Copy/pasted your formula, didn't work
4) Tried making adjustments (increased the numbers of "aaaa" to 27, adjusted the J1 reference to $I9, I9:J29, but nothing is working)

I appreciate your help. Let me know if there's any other info I could provide that would be helpful.
 
Upvote 0
I experimented a bit with merged cells, and I got this to work:

1) Select I9:J29
2) CF > New Rule > Use Formula
3) =SUMPRODUCT(--ISERROR(SEARCH("~"&MID(I9&REPT("a",27),ROW(INDIRECT("1:27")),1),"abcdefghijklmnopqrstuvwxyz0123456789")))

CF is a bit finicky, you have to get it just right. The I9 in the formula, without $s, must be the upper left cell in your range. If a space is allowed in the user name, add it to the allowed string.

Let me know if this works better!
 
Upvote 0
I experimented a bit with merged cells, and I got this to work:

1) Select I9:J29
2) CF > New Rule > Use Formula
3) =SUMPRODUCT(--ISERROR(SEARCH("~"&MID(I9&REPT("a",27),ROW(INDIRECT("1:27")),1),"abcdefghijklmnopqrstuvwxyz0123456789")))

CF is a bit finicky, you have to get it just right. The I9 in the formula, without $s, must be the upper left cell in your range. If a space is allowed in the user name, add it to the allowed string.

Let me know if this works better!

This works great! Thanks so much Eric :) I have no idea how it works, but I look forward to playing with it to figure it out.
 
Upvote 0
I'm glad it works for you! :cool:

If you paste the formula in a regular cell, you can use the Evaluate Formula button on the Formulas tab to walk through it. I use that a lot!
 
Upvote 0

Forum statistics

Threads
1,217,358
Messages
6,136,095
Members
449,991
Latest member
IslandofBDA

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