I m trying to pass in a data validation a range but im receiving an error. Any ideas?
Error in line:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=rng
Error:
Code:
Option Explicit
Sub test()
Dim rng As Range
Set rng = wsIndex.Range("A1:A5")
With wsIndex.Range("K1").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=rng
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
DataValidation won't accept Range
only a String
so you can do this:
Formula1:= "=" & rng.Address
because is expecting a formula.
Whilst you can reference a range (as per @Damian) you could also work with arrays:
Option Explicit
Sub test()
Dim lst As Variant
lst = Application.Transpose(wsIndex.Range("A1:A5"))
With wsIndex.Range("K1").Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(lst, ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.