Can Data Validation be populated as an Array

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
I had someone who is brilliant come up with this idea and it works like a charm but the problem is that the Array is in memory and when I close Excel and reopen it the Array is gone and the Data Validation is lost. Below is the code and you will see the array, "ary" is converted into the string, "S"

I am looking for a way to make this work when I reopen the file.

Code:
Sub DVMakerRightColumn()
    Dim col As Collection
    Set col = New Collection
    Dim N As Long, I As Long, S As String
    '
    ' get the data
    '
    With Sheets("Merchhier")
        N = .Cells(Rows.Count, "G").End(xlUp).Row
        For I = 8 To N
            v = .Cells(I, "G").Value
            On Error Resume Next
            col.Add v, CStr(v)
            On Error GoTo 0
        Next I
    End With
    '
    ' remove duplicates
    '
    ReDim ary(1 To col.Count) As Variant
    For I = 1 To col.Count
        ary(I) = col.Item(I)
    Next I
    '
    ' sort the array
    '
    Call ShellSort(ary)
    '
    ' build the data validaton string
    '
    S = Join(ary, ",")
    test1 = Len(S) '
    ' set up the data validation
    '
    With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=S
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
    End With
End Sub

Public Sub ShellSort(A() As Variant)
    Dim I As Long, J As Long, Low As Long, Hi As Long
    Dim Temp As Variant
  Low = LBound(A)
  Hi = UBound(A)
  J = (Hi - Low + 1) \ 2
  Do While J > 0
    For I = Low To Hi - J
      If A(I) > A(I + J) Then
        Temp = A(I)
        A(I) = A(I + J)
        A(I + J) = Temp
      End If
    Next I
    For I = Hi - J To Low Step -1
      If A(I) > A(I + J) Then
        Temp = A(I)
        A(I) = A(I + J)
        A(I + J) = Temp
      End If
    Next I
    J = J \ 2
  Loop
End Sub

Thanks,
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top