Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel data validation - regular expressions?

I have a column in an Excel 2007 spreadsheet where I want the data to be restricted to values "COM[1-4] or 1-65535". How can I get Excel to enforce this set of values?

TY, Fred

like image 783
fred basset Avatar asked Jan 16 '13 21:01

fred basset


2 Answers

Not very flexible, but you could use a Custom Data Validation, with this formula:

=OR(AND(LEN(A2)=4,LEFT(A2,3)="COM",MID(A2,4,1)>="1",MID(A2,4,1)<="4"),OR(AND(A2>=1,A2<=65535)))

I think this narrows it down to what you've specified in your comment.

like image 125
Doug Glancy Avatar answered Oct 14 '22 04:10

Doug Glancy


You will need to create a custom VBA function (or Macro) that supports Regular Expressions. I've personally done this, so I know it's possible :-)

If this is a one-time issue, it can be solved by using a complex FIND()/MID()/SUBSTITUTE(), etc. (this is my day job, actually), but I wouldn't recommend this unless you want to stare at a possible 5 line cell full of Excel functions.

Edit: I will update this if you have or need further info to offer.

Edit: Here's how you can use regular expressions as a function in Excel:

Function REFIND(strValue As String, strPattern As String, Optional blnCase As Boolean = True) As Boolean

    Dim objRegEx As Object
    Dim blnValue As Boolean

    ' Create regular expression.
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = strPattern
    objRegEx.IgnoreCase = blnCase

    ' Do the search test.
    blnValue = objRegEx.Test(strValue)

    REFIND = blnValue End Function
End Function
like image 42
Jonathan Avatar answered Oct 14 '22 06:10

Jonathan