Delphi: data validation in Excel, Formula1 not fix


I want to validate some cells (accept only values from a list).
            ExcelApp.Range['A1','A1'].EntireColumn.Validation.Add(Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:='=$D$1:$D$45');

Open in new window

With Delphi I fill in the values in column D. The number of values differs, so I can not set "Formula1:='$D$1:$D$45'" (45 may be different each time). How can I solve this that $D$45 is variable?

Thanks, Peter
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Rob HensonConnect With a Mentor Finance AnalystCommented:
Create a dynamic range name for your list of valid entries.

Assuming list on Sheet1 and nothing but list of entries in column D, the following can be used in the name manager to create a dynamic range:


You would then change your VBA to refer to the Named Range.

Rob H
formiAuthor Commented:
As I have the german version of Excel I had to change the , by ;

now it works with the following call (thank you for your help!):

ExcelApp.ActiveWorkbook.Names.Add('artgr', '=neu_nouveaux!$F$1:OFFSET(neu_nouveaux!$F$1;0;0;COUNTA(neu_nouveaux!$F:$F);1)');

I tried it with the param-names (Name:= and RefersToR1C1:=) inside as in other functions but I got an error "error in formula". Maybe you now why?? See below

ExcelApp.ActiveWorkbook.Names.Add(Name='artgr', RefersToR1C1:='=neu_nouveaux!$F$1:OFFSET(neu_nouveaux!$F$1;0;0;COUNTA(neu_nouveaux!$F:$F);1)');
Rob HensonFinance AnalystCommented:
The OFFSET function should replace the whole if the Refers to formula, not $F$1:OFFSET(...).

The offset function creates the range.
formiAuthor Commented:
ok, when I use OFFSET only it works if I use RefersTo (not RefersToR1C1. Thanks
All Courses

From novice to tech pro — start learning today.