Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Tue Jul 16, 2024 7:12 am

how to set selected index for dropdown already present in excel .

Code -
Define the data for the dropdown list (assuming it's "Option 1", "Option 2", "Option 3")
e.g.
Dim dropdownOptions As String() = {"Option 1", "Option 2", "Option 3"}

We are accessing the dropdown like below and set the value--
worksheet.Range("B9").value = "Option 2"


When we assign this drodown value , it is adding as text to dropdown cell, and not the item selected in dropdown.


Please give us proper example how to access the dropdown form excel worksheet and set the value at runtime.


I have gone through below documentation link for same -- But not getting info for setting the value of dropdown at runtime
https://www.e-iceblue.com/Knowledgebase ... 20property.

shekhar2024
 
Posts: 4
Joined: Fri May 24, 2024 11:51 am

Tue Jul 16, 2024 9:13 am

Hello,

Thanks for your inquiry.
Sorry, based on your current description, I am not quite sure what your specific needs are. Do you mean that you set a value for the cell that is not in the drop-down list, but it can still be set successfully? If not, please write back and tell us more details about your issue, and if possible some screenshots might help. Thanks a lot.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 451
Joined: Mon Dec 27, 2021 2:23 am

Thu Jul 18, 2024 12:45 pm

Hi Team ,

I have one dropdown list in excel, which having items {"Option 1", "Option 2", "Option 3"}. and which have the current Item selected is -> "Option 1"


Now at runtime i want to set the dropdown selected item to Option 3 .

We are accessing the dropdown using address like below and set the value--
worksheet.Range("B9").value = "Option 2"

How to achieve that, please send me code snippet.

shekhar2024
 
Posts: 4
Joined: Fri May 24, 2024 11:51 am

Mon Jul 22, 2024 12:07 pm

Hello,

Thanks for your reply.
Please refer to the code below for testing. If you have any questions, please feel free to reply.
Code: Select all
Dim workbook As New Workbook()

Dim worksheet As Worksheet = workbook.Worksheets(0)

' Create a string array
Dim values As String() = New String() {"Option 1", "Option 2", "Option 3"}

worksheet.Range("B9").DataValidation.Values = values
' Set the value
worksheet.Range("B9").Value = values(2)

workbook.SaveToFile("DropdownListCreatedFromArray.xlsx", ExcelVersion.Version2016)

workbook.Dispose()

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 451
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.XLS