I have a situation where a defined table structure changes based on a control type (for my instance it is a Product Brand). In that Table there are two column/fiels that will need to be used to populate two Data-Validation cells on the same worksheet. These cells are above the Table, and “ideally” would be used to filter the Table’s information based on the data populating the Data-Validation cells.
Data-Validation List Method Logic:
Dim str As String
str = Join(UniqueValues(ws,srcrng), ",")
Dim val As Excel.Validation
Set val = range(destrng).Validation
val.Delete
val.Add xlValidateList, xlValidAlertStop, xlBetween, str
- ws: Worksheet where @srcrng is located
- srcrng: Range to extract the Unique values from
- destrng: Range where the Data-Validation will be attached to. This will be controlled to be only a single cell and error out if has more than 1 row and 1 column.
The Above method is encapsulated in a method which is passed two parameters Table1[Column1] and DataValidationCell1 in VBA.
Just to stave off any questions, here is the method UniqueValues logic:
Function UniqueValues(ws As Worksheet, col As String) As Variant
Dim rng As range: Set rng = ws.range(col)
Dim dict As New Scripting.Dictionary
If Not (rng Is Nothing) Then
Dim cell As range, val As String
For Each cell In rng.Cells
val = CStr(cell.Value)
If InStr(1, val, ",") Then
val = Replace(val, ",", Chr(130)) <-- Handles the fact i have commas in the field
End If
If Not dict.Exists(val) Then
dict.Add val, val
End If
Next cell
End If
'Return value as Variant Array
UniqueValues = dict.Items
End Function
Questions:
- How do i account for Data-Validation changes?
- Can i use Data-Validation to Filter a Table?
- Since Pivot-Table is not a plausible option, due to the nature of the information, and if Data-Validation Filtering is not functionally possible, what alternative do i have?
On #3, i would like to not place a Drop-Down List, if at all possible.
Data-Validation is a way to restrict what kind of data can be entered in a cell (or a range of cells) http://office.microsoft.com/en-us/excel-help/apply-data-validation-to-cells-HP010072600.aspx
So your question is not specific to data validation. Your question seems to be about filtering a table based on the values entered in two cells at the top of that table (that happen to have data validation applied to them). Am I correct? If so: you are going to need to run a macro everytime those data validation cells are changed. You could make this automatic with Sub Worksheet_Change(ByVal Target As Range) or you could have an “update filters” button that would be slightly less “cool”. Either way, you simply need to link in a macro with those two cells and have them update the filter on the table based on the values of those two cells.
Here is an example of a macro that would do something like I described:
This is from: http://www.ozgrid.com/forum/showthread.php?t=85547
Hope this helps. Good Luck.