3

I have an array of data that is coming in to the VBA code from an external source. I want to be able to assign that data to use as a validation in a dropdown box in a cell in one of the sheets in this workbook. However, I do not want to copy that data into a sheet and then use a named range - there may be quite a lot of data, and that would not feel very efficient!

I'm sure there must be a way - but I haven't found one yet. Any ideas?

2 Answers 2

4
  1. Place the data in some text file delimiting it with comma eg(a,b,c).

  2. Read that data using VBA into a string variable eg ValidationList.

  3. Use some thing like this

    With Range("A1").Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=ValidationList
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
    End With

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

4 Comments

Here replace "A1" with the cell you want to validate.
Thanks Thunder for the quick and accurate answer. The only issue with it is having to change the array into a comma separated string, which makes it a little more wordy. However it does what I need - thanks!
It may also be useful to add the line .Delete after With Range("A1").Validation to remove any previous validation in that cell, rather than cause an error.
This works on Excel 2007 for me, but not 2003. I can't believe it is that recent of a feature - is there something I am missing?
0

Here's a little trick I used, in this "list" is an ArrayList:

Dim ValidateList As String
For Each x In list
ValidateList = ValidateList + x + Chr(44)
Next
 With Sheets(yoursheet).Range(yourCell).Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
     xlBetween, Formula1:=ValidateList
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

Might seem a bit crude, but I think it works without any problems :)

Comments

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.