Solved

Validate version numbers in Excel.

Posted on 2013-05-22
11
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

734 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