Cosmos75
Registered User.
- Local time
- Today, 10:51
- Joined
- Apr 22, 2002
- Messages
- 1,281
How do I tell if a string is a valid range of values, like when you tel your printer what pages to print? e.g. 1-2, 3-4.
But I want to include negative and decimal numbers.
Here's what I've got but it doesn't work perfectly.
But I want to include negative and decimal numbers.
Here's what I've got but it doesn't work perfectly.
Code:
Public Type udtStrRange
Valid As Boolean
Lower As Double
Upper As Double
End Type
Public Function IsRange(strValue As String) As udtStrRange
Dim posdash As Long
Dim dblLeft As Double
Dim dblRight As Double
Dim RE As Object
Set RE = CreateObject("vbscript.RegExp")
RE.Pattern = "^[-0-9]+-[-0-9]$" '"-4--1" [COLOR=DarkGreen]'Single Numbers ONLY[/COLOR]
strValue = RemoveSpaces(strValue)
IsRange.Valid = RE.Test(strValue)
Set RE = Nothing
If IsRange.Valid Then
posdash = InStr(2, strValue, "-", vbTextCompare)
dblLeft = Left(strValue, posdash - 1)
dblRight = Right(strValue, Len(strValue) - posdash)
Select Case (dblLeft < dblRight)
Case True
IsRange.Lower = dblLeft
IsRange.Upper = dblRight
Case False
IsRange.Lower = dblRight
IsRange.Upper = dblLeft
Case Else
IsRange.Lower = 0
IsRange.Upper = 0
End Select
Else
IsRange.Lower = 0
IsRange.Upper = 0
End If
End Function