Consulting

Results 1 to 6 of 6

Thread: Converting Criteria Listed in Excel For Data Validation

  1. #1

    Converting Criteria Listed in Excel For Data Validation

    I am having problems with creating a dropbox with the data validation. I have 120 criteria sets, which described in one cell. For example, Cell A1 contains "x,y,Max(x,y)". When I use the following code:
    [VBA]
    With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=ValCell
    End With
    [/VBA]This makes 4 choices in the dropbox: x, y, Max(x, and y) where there should be only one. Is there anyone else who had to do it? I am having a really hard time. Thanks!

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,776
    What did you want the list to be?

    Would putting "x" in A1, "y" in A2, "Max(x,y)" in A3 and
    [VBA].Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=$A$1:$A$3"[/VBA] do what you want?

  3. #3

    Criteria is in Excel

    The criteria is in one cell and I have to keep it that way since someone else is populating the criteria list. I can't convert the text to columns. Is there a way to that in VBA?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    You have two major difficulties, first the value will be seen as as string, not a formula (as I assume the max to be), and you have a delimiter (,) embedded within that formula.

    If you were to do it in VBA I think it would have to be parsed bit by bit, and it would need a lot of rules and processing code. Too hard methinks.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,776
    This should do what you need it to do. Where supported, a scripting dictionary might be used instead of the loop.
    With Range("A1").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
            Formula1:=SpecialCommasInsideParenthesis(valCell.Value)
    End With
    
    Function SpecialCommasInsideParenthesis(inputString As String) As String
    Dim i As Long, oneChr As String, workingInside As Long
        For i = 1 To Len(inputString)
            oneChr = Mid(inputString, i, 1)
            If oneChr = "," And CBool(workingInside) Then
                oneChr = Chr(226)
            End If
            SpecialCommasInsideParenthesis = SpecialCommasInsideParenthesis & oneChr
            workingInside = workingInside - (oneChr = "(") + (oneChr = ")")
        Next i
    End Function

  6. #6

    Thank you!

    Thanks, Mike. That's exactly that I was looking for. Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •