Setting validation via VBA in excel fails when using variable

后端 未结 6 2050
野趣味
野趣味 2021-01-06 07:56

I am trying to set the data validation for a range of cells using VBA. I get a run-time error 1004 (so helpful) \"Application defined or object defined error\" with this cod

6条回答
  •  既然无缘
    2021-01-06 08:37

    I have referenced named location using the INDIRECT function and everything run perfect. Users however reported some regular, seemingly random errors and from log file I saw that this Validation.add is causing the problem. The thing is, that the Excel tool is running in background while different Excel file operated by user may have focus when this operation is executed.

    Didn't test the following Code. Hope it helps.

    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= xlBetween, Formula1:="=INDIRECT(""[" & ThisWorkbook.Name & "]" & sht_sfDocumentation.Name & "!" & gstrRANGE_NamedTableRange & """)"
    

提交回复
热议问题