Solved

Validate version numbers in Excel.

Posted on 2013-05-22
11
308 Views
Last Modified: 2013-07-04
I have a column in Excel that contains version numbers in the format 'n.n' where there can technically be any number of 'n' numbers e.g. 'n.n' is valid, 'n.n.n' is valid and so on. Using Excel data validation how can I create a custom validation rule to check for values that do not comply to this.
0
Comment
Question by:Blowfelt82
  • 5
  • 5
11 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 39188267
I don't think that data validation can easily handle this, but here's a macro you can run on the current cell (which could be extended to apply to the whole sheet or a predetermined range):

Sub ValidateCurrentCell()
    Dim strVersions() As String
    Dim bValidVersion As Boolean
    Dim iVersionCntr As Integer
    
    strVersions = Split(ActiveCell.Value, ".")
    bValidVersion = True
    For iVersionCntr = 0 To UBound(strVersions)
        If Val(strVersions(iVersionCntr)) = 0 And Not strVersions(iVersionCntr) Like String(Len(strVersions(iVersionCntr)), "0") Then
            bValidVersion = False
        End If
    Next
    
    If Not bValidVersion Then
        MsgBox "The current cell does not contain a valid version in the format of ##[.##[.##]...]"
    Else
        MsgBox "Version is valid"
    End If
End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:redmondb
ID: 39188363
Hi, Blowfelt82.

Please try the following Data Validation formula...
=ISNUMBER(1*SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ","a"),"..","a"),".",""))

Open in new window

Regards,
Brian.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39188392
Great formula Brian.  I guess you should also substitute out the "-", like -11.-11

For mine, to handle this, on line 9 change
...= 0

to ...<= 0
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 26

Expert Comment

by:redmondb
ID: 39188840
Thanks, rspahitz. Frankly, I didn't consider that negative numbers were valid.

It's not enough to just substitute "-", otherwise "8-8" would be valid, so...
=ISNUMBER(1*SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B2,".-","")," ","a"),"..","a"),".",""))

Open in new window

...an initial "-" being OK.

Regards,
Brian.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39188874
Good point about 8-8 (so ".-" is good), but also the initial "-" if it's there.  I guess that comes down to the source of all of this potentially bad data so maybe all the extra effort is not needed.

Also, I tried "tricking" it with other cases and couldn't find any, so that should wrap it up, but I still stand by my initial comment "I don't think that data validation can easily handle this" but I love your idea of thinking out of the box to handle it. :)
0
 
LVL 26

Expert Comment

by:redmondb
ID: 39188946
rspahitz,

I guess our boxes are in different places - I thought of the formula immediately, but a macro like yours would have taken me time.

BTW, "other cases" - try "1..2".    :)

Regards,
Brian.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39189028
>try "1..2".

I saw that case and decided that "nothing" (the value between two dots) was essentially zero and therefore valid...but as I think further, it should probably be excluded by replacing the IF with the following:

        If strVersions(iVersionCntr) = "" _
        Or Val(strVersions(iVersionCntr)) <= 0 _
        And Not strVersions(iVersionCntr) Like String(Len(strVersions(iVersionCntr)), "0") Then

Open in new window


--
Rob
0
 
LVL 26

Expert Comment

by:redmondb
ID: 39189108
Still OK with "8-8"?!
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 39189333
Very well then...here's a cleaned up version, turned into a function, which, I suppose, you could use check for true/false like this: =ValidateCurrentCell(A1)

where A1 is the cell to check

Function ValidateCurrentCell(SomeCell As Range) As Boolean
    Dim strVersions() As String
    Dim bValidVersion As Boolean
    Dim iVersionCntr As Integer
    
    strVersions = Split(SomeCell.Value, ".")
    bValidVersion = True
    For iVersionCntr = 0 To UBound(strVersions)
        If strVersions(iVersionCntr) = "" _
        Or Not strVersions(iVersionCntr) Like String(Len(strVersions(iVersionCntr)), "#") Then
            bValidVersion = False
            Exit For
        End If
    Next
    
'    If Not bValidVersion Then
'        MsgBox "The current cell does not contain a valid version in the format of ##[.##[.##]...]"
'    Else
'        MsgBox "Version is valid"
'    End If
    ValidateCurrentCell = bValidVersion
End Function

Open in new window

0
 
LVL 26

Expert Comment

by:redmondb
ID: 39189373
Nice, Rob. That gives Blowfelt82 maximum flexibility.
0
 

Author Closing Comment

by:Blowfelt82
ID: 39299570
Thanks for the help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question