如何通过引用命名范围的vba代码添加验证列表? 这样列表将包含命名范围的值? 我可以这样做,比如Formula1:=“= $ A $ 1:$ A $ 10”但我怎样才能给出命名范围?
答案 0 :(得分:13)
您可以使用以下代码:
'Create the named range (if not done already)
ActiveWorkbook.Names.Add Name:="listdata", RefersTo:= "=Sheet2!$A$1:$A$10"
'Set a validation list on the cells that will refer to the named range
With Range("A1:A100")
With .Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=listdata"
End With
End With