简体   繁体   中英

How do I select an item in an Excel drop down in C#?

I'm working on a little program (C#, using Visual Studio 2015) that populates an Excel file. This Excel sheet contains a drop down list, which is populated with data from a different worksheet in the same file.

Currently you have to select the desired item by hand, so it can set a number of variables. The sheet has a macro with some calculations that need these variables. The program I'm making is going to automate the process, so I need to select the right item in the list programatically and run the macro afterwards. This is where I'm stuck. I can add values to other cells and the macro runs fine, but it's using the default list value.

I've been looking for a solution, but can't get it to work. This does nothing for me: Changing the value in a drop down list Excel C#

worksheet.Cells[4, 3] = "5";

where "5" is an arbitrary index just to get it to change. Doesn't work. Replacing "5" by "actual string value" doesn't work either.

I'm using the Microsoft.Office.Interop.Excel.Application object to control the file.

How do I select an item from the list? Either by matching string or by index, both will do.

Got it to work! The problem was that the cell value got overridden by a value in another worksheet.. Changing this one in stead of the actual drop down list did the trick.

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