开发者

VBA excel code:cannot give named range to formula1 in validation

开发者 https://www.devze.com 2023-03-21 23:54 出处:网络
How开发者_C百科 can i add a validation list by vba code that will refer to a named range? So that the list will contain the values of a named range?

How开发者_C百科 can i add a validation list by vba code that will refer to a named range? So that the list will contain the values of a named range? I can do this like Formula1:="=$A$1:$A$10" but how can i give a named range?


You can use the following code:

'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
0

精彩评论

暂无评论...
验证码 换一张
取 消