Forum Discussion
Adding data validation in Excel Addin
Hi all,
I'm trying to add dependent dropdown lists in code:
String formula2 = "=INDIRECT(C2)";
wsN.Range["D2"].Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertInformation, XlFormatConditionOperator.xlBetween, formula2);
(C2 is a dropdown list with items that correspond to named ranges)
If I use this formula in the Data/Data validation/Settings Source it works.
Does somebody know how I should format the formula to make it work in code?
In your code, you're trying to use the formula =INDIRECT(C2) directly as the validation formula. However, when setting data validation programmatically in Excel, you can't directly reference cell values in the same way as you do in the Excel formula bar.
Instead, you need to construct the formula dynamically using the appropriate syntax for referring to cell values. Here's how you can modify your code to achieve this:
Code is untested please backup your file.
using Excel = Microsoft.Office.Interop.Excel; // Assuming wsN is a reference to your worksheet Excel.Range range = wsN.Range["D2"]; // Construct the formula dynamically string formula2 = "=INDIRECT(\"'\" & C2 & \"'\""; // Add data validation to the range range.Validation.Add( Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertInformation, Excel.XlFormatConditionOperator.xlBetween, formula2, Type.Missing );
In this modified code:
- We construct the formula as a string, concatenating the parts together using &.
- We use double quotes (\") to properly enclose the sheet name in the INDIRECT function. Note the additional single quotes (') around the sheet name.
- We pass the formula as an argument to the Validation.Add method.
With these adjustments, the formula should work properly when set programmatically in your Excel add-in. Make sure to adjust the code according to your specific requirements and environment. The text, steps and formulas was created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.
- NikolinoDEGold Contributor
In your code, you're trying to use the formula =INDIRECT(C2) directly as the validation formula. However, when setting data validation programmatically in Excel, you can't directly reference cell values in the same way as you do in the Excel formula bar.
Instead, you need to construct the formula dynamically using the appropriate syntax for referring to cell values. Here's how you can modify your code to achieve this:
Code is untested please backup your file.
using Excel = Microsoft.Office.Interop.Excel; // Assuming wsN is a reference to your worksheet Excel.Range range = wsN.Range["D2"]; // Construct the formula dynamically string formula2 = "=INDIRECT(\"'\" & C2 & \"'\""; // Add data validation to the range range.Validation.Add( Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertInformation, Excel.XlFormatConditionOperator.xlBetween, formula2, Type.Missing );
In this modified code:
- We construct the formula as a string, concatenating the parts together using &.
- We use double quotes (\") to properly enclose the sheet name in the INDIRECT function. Note the additional single quotes (') around the sheet name.
- We pass the formula as an argument to the Validation.Add method.
With these adjustments, the formula should work properly when set programmatically in your Excel add-in. Make sure to adjust the code according to your specific requirements and environment. The text, steps and formulas was created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.
- RBiemsCopper Contributor
- NikolinoDEGold ContributorWithout the file (without sensitive data) and detailed information, it is difficult for me to be able to help. Detailed information about the entire code, Excel version, operating system, storage medium, Excel file extension, etc. always helps to get a suitable solution suggestion.