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.
Thanks,
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,