Public Sub NameToValList( _
ByVal ArrayConstant_Name As Name, _
ByVal ValidationList_TargetRange As Range _
)
Dim vArr() As Variant
Dim i As Long, lArrayItemsCount As Long
lArrayItemsCount = UBound(Split(ArrayConstant_Name.Value, ",")) + 1&
For i = 1& To lArrayItemsCount
ReDim Preserve vArr(i - 1&)
vArr(i - 1&) = Trim(Replace(Replace(Replace(Replace(Split(ArrayConstant_Name.Value, ",") _
(i - 1&), Chr(34&), ""), "=", ""), "{", ""), "}", ""))
Next i
With ValidationList_TargetRange.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(vArr, ",")
End With
End Sub
NameToValList Application.Names("TYPE_YOUR_ARRAY_CONSTANT_NAME_HERE"), Range("TYPE_YOUR_DATAVALIDATION_RANGE_ADDRESS_HERE")
NameToValList Application.Names("Products"), Range("B2")
Can you use vba ? If so, see if this routine works for you :
In a Standard Module:
VBA Code:Public Sub NameToValList( _ ByVal ArrayConstant_Name As Name, _ ByVal ValidationList_TargetRange As Range _ ) Dim vArr() As Variant Dim i As Long, lArrayItemsCount As Long lArrayItemsCount = UBound(Split(ArrayConstant_Name.Value, ",")) + 1& For i = 1& To lArrayItemsCount ReDim Preserve vArr(i - 1&) vArr(i - 1&) = Trim(Replace(Replace(Replace(Replace(Split(ArrayConstant_Name.Value, ",") _ (i - 1&), Chr(34&), ""), "=", ""), "{", ""), "}", "")) Next i With ValidationList_TargetRange.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(vArr, ",") End With End Sub
Usage:
NameToValList Application.Names("TYPE_YOUR_ARRAY_CONSTANT_NAME_HERE"), Range("TYPE_YOUR_DATAVALIDATION_RANGE_ADDRESS_HERE")
An example :
Where Products is the name of the array constant Name and B2 is the cell to add the validation list to... Adapt as required.VBA Code:NameToValList Application.Names("Products"), Range("B2")
NameToValList Application.Names("Products"), Range("B2")
Public Sub CreateValidationListInActiveCell()
' This creates the defined Name "Cars" and adds the 3 values
ActiveWorkbook.Names.Add "Cars", Array("BMW", "Audi", "VW")
' This calls Jaafar's code that pulls the values from "Cars"
' and adds them to a data validation list in the ActiveCell
NameToValList Application.Names("Cars"), ActiveCell
End Sub
One hacky trick that may work is maybe placing an array fomula in a column (say Col A:A) in a hidden worksheet. The array formula would be like ={TRANSPOSE(Cars)}.I want to be able to create a defined name and use it directly in a data validation list... and that way any updates made to the defined name would automatically update the list. Seems like you should be able to do it, but I have not found a way yet.
Option Explicit
Private Sub Worksheet_Calculate()
'Automatically update the validation list in cell Sheet1!A1.
NameToValList Application.Names("cars"), Sheet1.Range("A1")
End Sub
Private Sub NameToValList( _
ByVal ArrayConstant_Name As Name, _
ByVal ValidationList_TargetRange As Range _
)
Dim vArr() As Variant
Dim i As Long, lArrayItemsCount As Long
lArrayItemsCount = UBound(Split(ArrayConstant_Name.Value, ",")) + 1&
For i = 1& To lArrayItemsCount
ReDim Preserve vArr(i - 1&)
vArr(i - 1&) = Trim(Replace(Replace(Replace(Replace(Split(ArrayConstant_Name.Value, ",") _
(i - 1&), Chr(34&), ""), "=", ""), "{", ""), "}", ""))
Next i
With ValidationList_TargetRange.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(vArr, ",")
End With
End Sub
I too wanted to do exactly the same...Jaafar's VBA "Usage" line assumed that you already had a defined Name with an array in it called "Products".
If you want to create the defined Name in code and then pass it in to his code, you would add something like this:
VBA Code:Public Sub CreateValidationListInActiveCell() ' This creates the defined Name "Cars" and adds the 3 values ActiveWorkbook.Names.Add "Cars", Array("BMW", "Audi", "VW") ' This calls Jaafar's code that pulls the values from "Cars" ' and adds them to a data validation list in the ActiveCell NameToValList Application.Names("Cars"), ActiveCell End Sub
If you add this code along with Jaafar's code, you will create both the "Cars" defined name with the values you asked for, and then you will be using those same values in a data validation list for the current ActiveCell.
However... the data validation list doesn't actually run off of the defined name. What Jaafar is doing here only takes the values out of the name and uses them for the list. In other words, if you were to update the "Cars" name in the Name Manager and add another car ("Mercedes"), the data validation list would not be updated with the new value until you run Jaafar's code again, because the List is actually just pointing to an array of strings and not the Name.
I came across this because I want to be able to create a defined name and use it directly in a data validation list... and that way any updates made to the defined name would automatically update the list. Seems like you should be able to do it, but I have not found a way yet.
The previous solution needs a separate sheet/ array in vba to be created. But I wanted to define it directly in defined name.create a defined name and use it directly in a data validation list... and that way any updates made to the defined name would automatically update the list.
Jaafar's VBA "Usage" line assumed that you already had a defined Name with an array in it called "Products".
If you want to create the defined Name in code and then pass it in to his code, you would add something like this:
VBA Code:Public Sub CreateValidationListInActiveCell() ' This creates the defined Name "Cars" and adds the 3 values ActiveWorkbook.Names.Add "Cars", Array("BMW", "Audi", "VW") ' This calls Jaafar's code that pulls the values from "Cars" ' and adds them to a data validation list in the ActiveCell NameToValList Application.Names("Cars"), ActiveCell End Sub
If you add this code along with Jaafar's code, you will create both the "Cars" defined name with the values you asked for, and then you will be using those same values in a data validation list for the current ActiveCell.
However... the data validation list doesn't actually run off of the defined name. What Jaafar is doing here only takes the values out of the name and uses them for the list. In other words, if you were to update the "Cars" name in the Name Manager and add another car ("Mercedes"), the data validation list would not be updated with the new value until you run Jaafar's code again, because the List is actually just pointing to an array of strings and not the Name.
I came across this because I want to be able to create a defined name and use it directly in a data validation list... and that way any updates made to the defined name would automatically update the list. Seems like you should be able to do it, but I have not found a way yet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
NameToValList Application.Names("Products"), Range("B2")
End Sub
I would be wary to use the selection_change event as this would run everytime a cell is selected and you will lose the UNDO functionality all the time. This is an undesired side effect.Hey I actually found a way out...
VBA Code:Private Sub Worksheet_SelectionChange(ByVal Target As Range) NameToValList Application.Names("Products"), Range("B2") End Sub
use it in this was... everytime there is selection change, the function triggers and updates the list.
Thanks a lot @Jaafar Tribak and @jputman !!
You guys are amazing