I am looking for a way to make a data validation list pull from a filtered table. Ex. I have a sheet called customers with a table named CustomerList column A=Customer B=Address C=City D=State On another sheet named Quote I have cell C13 for customer name which has a data validation list to a dynamic range of Sheet Customers column A Customer. In my list it shows all 1800 customers even if i filter the table to show only customers in a certain state. I would like to be able to set filters on the table to sort my customers and have my data validation list only show the customers shown on the filtered list. For the life of me I can not figure this out. Any help would be greatly appreciated. TIA.
1 Answer
In sheet Customers, pick some cell and enter:
=SUBTOTAL(103,A:A)
This formula will be re-calculated every time the filter is changed for column A.
In the Customers worksheet code area, install the following event macro:
Private Sub Worksheet_Calculate()
Call makeDV
End Sub
In a standard module, install the following code:
Public DVList As String
Public Sub makeDV()
Dim A As Range, r As Range
Dim c As Collection, v As Variant
Set c = New Collection
Set A = Intersect(Range("A2:A" & Rows.Count), ActiveSheet.UsedRange).Cells.SpecialCells(xlCellTypeVisible)
DVList = ""
On Error Resume Next
For Each r In A
v = r.Value
If v <> "" Then
c.Add v, CStr(v)
If Err.Number = 0 Then
DVList = DVList & "," & v
Else
Err.Clear
End If
End If
Next r
If Len(DVList) <> 0 Then DVList = Mid(DVList, 2)
On Error GoTo 0
Dim Quote As Worksheet
Set Quote = Sheets("Quote")
With Quote.Range("C13").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=DVList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Whenever the filter is modified on sheet Customers, the data validation for cell C13 on sheet Quotes will up updated.
10 Comments
Jason Adams
this sort of works. when i have a customer name that has a comma in it say Badger Pressure Control, LLC in my drop down it shows as individual entries. Badger Pressure Controls and LLC as seperate entries. Also when i sort by texas its stopping the list in the M's for some reason. Think its something to do with the way the customer may be named
Jason Adams
When filtering by TX it stops in my list at Mountain Supply when in the customer list it is mountain supply service, not sure why though. i removed that entry and it stops on the next one. when sorted by texas i have a count of 640 in that cell and the data validation list seems to be stopping at a count of 348 if that helps. although when i have no filters it shows all 1800 customers
Jason Adams
if i have no filters it gives me error when i clear the filters and whole table is displayed ` .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:=DVList`
Jason Adams
in debug mode when hovering about it xlValidateList=3 xlValidAlertStop=1 xlBetween=1 if that helps at all
Gary's Student
@JasonAdams You have uncovered a real bug. The items in a comma-separated-list cannot contain commas themselves. We need to use a helper column instead.....are you O.K. with using a helper column??
|