I want to create a drop-down list in
My range of values for the drop-down should come from
Sheet2. However, my code below is not working, it runs an error 1004 at
Add xlValidateList, xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7" I suspect it has something to do with this line…
Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7" . Does anyone know how I can add two ranges into my
Also, I dont want the blanks in the ranges to appear in the Drop-down list but
.IgnoreBlank = True still shows the blanks in the drop-down list.
This is my code thus far, any help is genuinely appreciated:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("E5")) Is Nothing Then With Range("e5").Validation .Add xlValidateList, xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7" .IgnoreBlank = True .InCellDropdown = True .ErrorTitle = "Warning" .ErrorMessage = "Please select a value from the drop-down list available." .ShowError = True End With End If End Sub
To add multiple columns data, use a unique collection and then feed that to the DV in a comma delimited string. See this example. Change as applicable.
Sub Sample() Dim col As New Collection Dim rng As Range Dim i As Long Dim DVList As String '~~> Loop through the data range For Each rng In Sheet2.Range("A1:A5,C1:C5") '~~> Ignore blanks If Len(Trim(rng.Value)) <> 0 Then '~~> Create a unique list On Error Resume Next col.Add rng.Value, CStr(rng.Value) On Error GoTo 0 End If Next rng '~~> Concatenate with "," as delimiter For i = 1 To col.Count DVList = DVList & col.Item(i) & "," Next i '~~> Feed it to the DV With Sheet1.Range("E5").Validation .Delete .Add Type:=xlValidateList, _ AlertStyle:=xlValidAlertStop, _ Formula1:=DVList End With End Sub
the property Formula1:=”Expression” does not allow splitted tables. As Siddharth Rout wrote – as a work aorund – first you have to collect all values in a single table or a string.
Also the IgnoreBlank property does not ignore blanks, but is considering the cell value as valid even the cell content is blank. So, while collecting table content skip blank cells.