简体   繁体   中英

Trying to link drop down menus with if statements in excel with C# code

I have been trying to use Validation.Add and if statements for a drop down list in cell A2 to be dynamic and add certain arraylists options dependent on cell A1.

I also cant find any code for it anywhere apart from maybe an Office2010 reference but I dont think it would work with the 2019 version.

       x.Range["A2"].Validation.Delete();
       x.Range["A2"].Validation.Add(Excel.XlDVType.xlValidateList, Type.Missing, 
       Excel.XlFormatConditionOperator.xlBetween, 
       "=if(A1="+ DO.quote + "XDDoor" + DO.quote+ "," + DO.quote + "Pass" + 
       DO.quote+ "," + DO.quote + string.Join(",", 
       arrayList.ToArray()) + DO.quote+ ")"
        );

I had to put the formula into a string before adding it into the Add parameter. after this i am going to alter each section of the string dynamically with parameter names.

            for (int i = 1; i <= StandardDoor.Count; i++)
            {
                x.Range["E"+i].Value = StandardDoor[i-1];
            }
            for (int i = 1; i <= XDDoor.Count; i++)
            {
                x.Range["F" + i].Value = XDDoor[i - 1];
            }
            for (int i = 1; i <= SR2Door.Count; i++)
            {
                x.Range["G" + i].Value = SR2Door[i - 1];
            }
            for (int i = 1; i <= SR3Door.Count; i++)
            {
                x.Range["H" + i].Value = SR3Door[i - 1];
            }

string xform = "=IF(B2=" + DO.quote + "StandardDoor" + DO.quote + "," 
                + "E1:E4" + ",IF(B2="+ DO.quote + "XDDoor" + DO.quote +"," + "F1: F4" +
                ",IF(B2=" + DO.quote + "SR2Door" + DO.quote + "," 
                + "G1: G3" + ",IF(B2=" + DO.quote + "SR3Door" + DO.quote + "," 
                + "H1: H3" + "," + "I1: I3" + "))))";
x.Range["B4"].Validation.Delete();
x.Range["B4"].Validation.Add(Excel.XlDVType.xlValidateList, 
                 Excel.XlDVAlertStyle.xlValidAlertStop, 
                 Excel.XlFormatConditionOperator.xlBetween, 
                 xform, Type.Missing);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM