Cells can only contain characters 0 thru 9

keith05281967

Board Regular
Joined
May 6, 2011
Messages
68
Greetings Coders,

How can I have vba loop thru a range and check that the contents only contain the characters 0 thru 9. I say characters because they aren't numbers, rather work id's. Usually formatted as general or text. So something like...


<TABLE style="WIDTH: 135pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=180><COLGROUP><COL style="WIDTH: 135pt; mso-width-source: userset; mso-width-alt: 6582" width=180><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 135pt; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17 width=180>870232</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>10213#6</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>9706371</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>20087964</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>20477341</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>20686054</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17></TD></TR></TBODY></TABLE>
If Cells (x,y) <> (0,1,2,3,4,5,6,7,8,9) then
msgbox "hey i found a non numeric character"

As I mentioned, the range is work ID's so IsNumeric doesn't apply.


thanks,
Keith
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Why does IsNumeric not apply?
Code:
Sub test()
Dim c As Range
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    If IsNumeric(c) Then
            If Int(c) <> c Then MsgBox "Invalid Entry Found in Cell " & c.Address(0, 0)
    Else
            MsgBox "Invalid Entry Found in Cell " & c.Address(0, 0)
    End If
Next
End Sub
 
Upvote 0
It could also be done like this:

Code:
Sub test()
Dim c As Range
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    If IsNonNumeric(c.Text) Then MsgBox "Invalid Entry found in Cell " & c.Address(0, 0)
Next
End Sub

Function IsNonNumeric(r As String) As Boolean
With CreateObject("vbscript.regexp")
    .Pattern = "\D"
    IsNonNumeric = .test(r)
End With
End Function
 
Upvote 0
Hi HotPepper,

You are right. IsNumeric will work after all. I was just using the wrong syntax on it before, but I got it working now. Thank you for all of the suggestions. My 1st attempt looked something like... If Cells(i, ColumnNumber).IsNumeric = False Then...but it didn't like that. Below is what i got working:


If (IsNumeric(Cells(i, ColumnNumber))) = False Then
Cells(i, ColumnNumber).Interior.ColorIndex = 4
intCounter3 = intCounter3 + 1
End If


thanks,
Keith
 
Upvote 0
... IsNumeric will work after all ...

Hi Keith

I would not use IsNumeric() to test for only digits. IsNumeric() tests if it is possible to convert the string into a number which is clearly not what you need.

Examples of string that test ok with IsNumeric() and that have characters other than digits:

11.
11.1
11E1
+234
-234
234-
234+
1,2,3,4
11 (with spaces after the 11)
11 (with spaces before the 11)

I'd use something like this, that tests if all the characters are digits (non empty string)

Code:
Sub AllDigits()
Dim s As String
 
s = "123#45"
Debug.Print s Like String(Len(s), "#")
End Sub
 
Upvote 0
To a point you are correct, for some of those, my first code correctly identified as being incorrect.

My 2nd code I posted found all the examples.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
Members
452,940
Latest member
rootytrip

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