0

I have a certain range of cells in excel where I want to apply data validation on another data validation.

I want the user to only have 3 options for data input within range of cells:

  1. either a number,
  2. a range of numbers or by
  3. choosing from a dropdown list that contains words and numbers.

I already implemented number 1 and 2 with the following function:

Function checkStr(ByVal str As String) As String

    Dim objRegEx As Object, allMatches As Object
    Set objRegEx = CreateObject("VBScript.RegExp")

    With objRegEx
        .MultiLine = False
        .IgnoreCase = False
        .Global = True
        .Pattern = "^\d+(-\d+)?$"
    End With


    Set allMatches = objRegEx.Execute(str)
    checkStr = (allMatches.Count > 0)

End Function

Since the above function will only allow numbers or a range of numbers to be inputted, any ideas on how to add a validation to allow values from a pre-defined list containing both words and numbers?

1
  • 1
    Why is the function returning String and not Boolean?
    – Vityata
    Commented Jul 12, 2018 at 9:18

2 Answers 2

1

I suggest to change the return As Boolean and then just filter the str against an array of valid list entries.

Function checkStr(ByVal str As String) As Boolean

    Dim objRegEx As Object, allMatches As Object
    Set objRegEx = CreateObject("VBScript.RegExp")

    With objRegEx
        .MultiLine = False
        .IgnoreCase = False
        .Global = True
        .Pattern = "^\d+(-\d+)?$"
    End With

    Set allMatches = objRegEx.Execute(str)


    Dim ValidList As Variant
    ValidList = Array("123", "456") 'your list of valid entries

    'check if either str is in the ValidList OR matches the regex
    If (UBound(Filter(ValidList, str)) > -1) Or (allMatches.Count > 0) Then
        checkStr = True
    End If

End Function

If the list of valid entries is in a range you can replace it with:

ValidList = WorksheetFunction.Transpose(Worksheets("SheetName").Range("A1:A10").Value)
7
  • Sorry if this may be a stupid comment but I'm not that knowledgeable in VBA. While copiling, an error is showing saying "compile error: Sub or Function not defined". I'm calling the function with the following code: If checkStr(Target.Value) = False Then Target.Value = "" End If
    – TryStudent
    Commented Jul 12, 2018 at 9:57
  • Is your Function checkStr in a module or in a worksheet? Try to make it a Public Function. Otherwise in which line does this error occur? If you use the list in a range there was a s missing in Worksheets("SheetName") it must be Worksheets not Worksheet (just corrected it) by bad.
    – Pᴇʜ
    Commented Jul 12, 2018 at 10:05
  • Making it a public function doesn't show any errors, however the code is not working. It's allowing values outside of the defined range and pattern. Updated the code too.
    – TryStudent
    Commented Jul 12, 2018 at 10:13
  • @TryStudent Well, it worked when I tested it. Can you give an example? What value did you use for str and what for ValidList?
    – Pᴇʜ
    Commented Jul 12, 2018 at 10:15
  • 1
    Copy pasted the same code into a new document and it worked. Thanks for your help really appreciate it :)
    – TryStudent
    Commented Jul 12, 2018 at 10:20
1

The list is taking values from some range. Thus, take the range of the list and use the Application.Match() to check whether the str is there:

Public Function checkStr(str As String) As Boolean

    Dim isItError As Variant
    isItError = Application.Match(str, Worksheets(1).Range("A1:A5"), 0)

    checkStr = Not IsError(isItError)

End Function

Application.Match() would return either error or true. Thus, your function can be fixed with Not IsError().


And if you want to compare Strings with Strings and Numbers as Numbers, try to pass the variable as Variant and let VBA decide what it is actually:

Public Function checkMe(someVar As Variant) As Boolean

    Dim isItError As Variant
    Dim formulaAddress As String

    With Range("C1").Validation
        formulaAddress = Right(.Formula1, Len(.Formula1) - 1)
    End With

    isItError = Application.Match(someVar, Range(formulaAddress))
    checkMe = Not IsError(isItError)

End Function

If you explicitly define the variable as a numeric or string, the other option would be excluded in the Application.Match() comparison:

?Application.Match("1",Array(1,2,3))
Error 2042
?Application.Match(1,Array(1,2,3))
1 
?Application.Match("1",Array("1","2","3"))
1 
?Application.Match(1,Array("1","2","3"))
Error 2042

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.