Forum Discussion
RBiems
Apr 03, 2024Copper Contributor
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.xlValidAlertInformatio...
- Apr 03, 2024
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.
NikolinoDE
Apr 03, 2024Gold 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.
- RBiemsApr 05, 2024Copper Contributor
- NikolinoDEApr 05, 2024Gold 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.
- RBiemsApr 09, 2024Copper ContributorHi NikolinoDE ,
Actually my original code worked, I made a stupid mistake by adding it to the first row as well and there wasn't a corresponding named ranged on that row because I replaced that with a header text...... braindead I know 🙂