Question : Data Validation with VBA without use of worksheet cells

Hi Experts,

In the attached file you will find the code below. It inserts Data Validation in Sheet1!A1 using only VBA. Unfortunately there appears to be a limit to the length of string that can be written to the DV 'Source' box. I have set up the macro so that you can determine the number of string elements (options) there are in the DV. You will find that round about 40 elements is the maximum before Excel crashes - perhaps not immediately but perhaps the next time the DV is tried - so please be careful. I would like the maximum number of elements/options in the DV string to be about 250.

So my question is: How can I get round this limitation?

The restrictions are: There must be no worksheet cells used for the Data Validation.

Can an array be used - if so, please demonstrate with code.

Patrick
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Sub DV()
Dim i As Long
Dim j As Long
Dim str1 As String

MsgBox "Beware this might crash Excel" & vbCrLf & "if you enter too large a number (>40)" & vbCrLf & "in the Inputbox." & vbCrLf & "Press CTRL+Break NOW, if you're concerned"
j = InputBox("Max elements in string", "Enter a number")

For i = 1 To j
    str1 = str1 & "TAR" & i & ","
Next i

str1 = Left(str1, Len(str1) - 1)

With Sheets("Sheet1").[a1].Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=str1
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

End Sub

Answer : Data Validation with VBA without use of worksheet cells

It's a max length issue.  If you use cells, validation can reference the list.  If you hardcode a list, it is saved by value so to speak and is therefore limited by length, and not neccessarily the number of delimited items.  You can examine the binary to see how the list is actually saved as a nullchar(0) delimited list.  What is the problem with naming the range that contains your list, hiding that specific worksheet, and then using the name in your validation list?
Random Solutions  
 
programming4us programming4us