Error check with optional space

C

corky_guy

So, I've tried this every way that I (in my limited mind) know how and
I am unable to do what I need to do.
Basically, I have a textbox in which users will enter either a single
digit or a range of digits from 0-7 separated by a single space
(i.e., 0 1 2 3 or 4 6 7)

The two problems I cannot overcome are:
1) The error checking that I'm currently using cannot distinguish
between "11" and "1". In other words, the error checking is looking
at each digit (11 = two ones) instead of the entire number (eleven)
and, thus, it's not triggering the error that an 11 is outside of the
valid range (0-7).

2) I cannot figure out how to allow spaces in the textbox while
maintaining the error checking


Any help you can provide is sincerely appreciated.

Thank you!
 
G

Greg Maxey

Corky,

I don't know exactly what you mean by "textbox," but whatever it is it has a
range.

Selection has a range also. If I type 1 3 11 4 7 6 and select it, I
could then run code like this:

Sub ScratchMacro()
Dim myRng() As String
Dim i As Long
myRng = Split(Selection.Range, " ")
For i = LBound(myRng) To UBound(myRng)
If Val(myRng(i)) > 7 Then Debug.Print "To Big"
Next i
End Sub

Basicly Split returns a zero base one dimensional array usig " " (space) as
the delimiter. Then you compare each subscript of the array to your
condition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top