1

can excel vba (function or Sub) return List of Data For "Data Validation >> List >> Source" ? Data Validation List (Drop Down List / combo Box) data source from my vba code Like:

= MyFunction()
return Like:
Apple,
Tool,Bag,Everything ...

more details: I need to search something with multi condition

Function myFunction(Rng1 As Range)
txtSearch As String
txtSearch = Rng1.Text
' do somting 

' return
' aApple,Tool,Bag,Everything ...
End Function

thanks for advice.

1

1 Answer 1

3

Please, try using the next function:

Function extractValidationList(vCell As Range) As String
    Dim strFormula As String
    
     strFormula = vCell.Validation.Formula1
        
     If left(strFormula, 1) = "=" Then
        Dim inputRange As Range, c As Range
        Set inputRange = Evaluate(strFormula)
        If inputRange.rows.count > inputRange.Columns.count Then
            'extract a 1D array from a range with more rows and one column and Join it
            extractValidationList = Join(Application.Transpose(inputRange.value), ", ")
        ElseIf inputRange.Columns.count > inputRange.rows.count Then
            'extract a 1D array from a range with more columns and one row and Join it
            extractValidationList = Join(Application.Transpose(Application.Transpose(inputRange.value)), ", ")
        End If
    Else
        Dim arrF, listSep As String
        listSep = Application.International(xlListSeparator)
        arrF = Split(strFormula, listSep)
        extractValidationList = Join(arrF, ", ")
    End If
End Function

It can be tested using the next Sub:

Sub testExtractValidationList()
   Debug.Print extractValidationList(ActiveCell) 'previously select the validated cell...
End Sub

Or using it UDF (User Defined Function), writing a formula in a cell:

=extractValidationList(C6)

where, C6 is the list validated cell, where from to extract the list...

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

3 Comments

@abdol-hamid Hosseiny Didn't you fins some time to test the above function? If tested, didn't it do what you need?
thanks for replay, but isn't Answer. Data > Data Tools > Data Validation > List > Source> = MyFunction()
I cannot get you... MyFunction(), without an argument will raise an error. You should try it as "=MyFunction(C6)". It can be also done in code. I thought this is something elementary and only supplied the function able to extract the list from any list validation type...

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.