Greetings all -- I'm trying to programatically generate a data collection workbook. It needs to include several lookup lists that will drive the acceptable choices in data validation for various columns. The lookup data changes over
time, so I can't just hard-code everything. (FWIW, I'm generating this workbook from Access.) Where I'm having trouble is creating a data validation where the Formula1 parameter is assigned to a variable, instead of hard coded the way the macro recorder
handles this.
The macro recorder produced the following: Selection.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=MyLookupList".
Trying to make this generic lead me to the following:
dim xlbk as Excel.Workbook
dim xlsht as Excel.Worksheet
dim strRangeName as string
dim rng as Excel.Range
'* xlbk and xlsht set upstream. named ranges established upstream
set rng = xlsht.columns(1)
strRangeName = "=MyLookupList"
rng.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween,Formula1:=strRangeName
'* this gives "error 1004" - first thing I tried
rng.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Chr$(34) & strRangeName &
Chr$(34) '* this runs OK, but doesn't give desired result -- second thing I tried
The second approach gives me a validation, but the acceptable value list is the literal string "=MyLookupList", instead of referring to the named range MyLookupList.
Any suggestions? I'm at my wit's end. Thanks... Mark