11

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

2
  • Do you mean, how do you get another column to only show those values which meet your criteria? Are you looking for range matching or exact matching? Can you give some examples of values you want to match, and some values that are close but shouldn't match? Commented Jan 16, 2013 at 21:05
  • 1
    Would like the value to be one of COM1, COM2, COM3, COM4 or an int 1-65535 Commented Jan 16, 2013 at 21:20

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 RegExp is overkill for what can be accomplished without VBA
9

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

4 Comments

If you could show how to do it in a macro that would be great.
I should be more specific. I've created functions. I don't care much for Macros (they have their place), but you'll still need to use a lot of the same custom VBA code. I'll attach the code above.
Perfect! Not sure why everyone else suggests regular excel functions whenever regex is suggested
@Foo_Chow if you can easily avoid using RegEx in Excel, do so; it will run faster without it. However, when something is dynamic enough that it requires regular expressions, it is a lot better than a list of raw text.
4

Use the below in Ribbon >> Data >> Data Validation. Then Allow = Custom and add the below in the Formula:

=OR(AND(E5 >= 1, E5 <= 65535), E5 = "COM1", E5 = "COM2", E5 = "COM3", E5 = "COM4")

Comments

0

For this specific question, using standard Data Validation would have worked with no VBA, Regex or formulas needed:

Allow: List Source: "COM1,COM2,COM3,COM4,1-65535"

This also provides a dropdown list in the cell to help user.

1 Comment

The list would need to contain the numbers 1 through 65535...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.