简体   繁体   中英

Getting the available choices in a dropdown cell in Excel using VB.NET

I'm looking into getting the all possible values of a dropdown cell in Excel using VB.NET. Basically here is my code...

Dim excel As New Microsoft.Office.Interop.Excel.Application()
Dim pvw = excel.Workbooks.Open(TextBox1.Text)
Dim wb As Microsoft.Office.Interop.Excel.Workbook = pvw
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = TryCast(wb.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)

'ws.Range("E9").Value is a dropdown cell and I want to loop through all the available values

wb.Close(True)
excel.Quit()
releaseObject(excel)

I just want to know what possible values there are in the dropdown so I can validate the data. How do I do this in either VB.NET or C#? Thank you so much.

Often times the easiest way to figure out the right VB code is to open up the macro (VBA) editor in the source Excel sheet and browse the properties. Doing that, I was able to see a Validation property on a range with a drop-down validation and saw the source range (in my case it was "=$A$1:$A$3" ) on the Formula1 property of that validation. So you could get the range reference easily:

String source_ref = ws.Range("E9").Validation.Formula1

From there, get the range object from the formula and loop through its cells to get the values.

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