I used Data Validation to create some drop down lists. Anyone know how to use VBA to select the first iterm in a drop down list?
I tried 'Split' function:
cell.Value = Split(cell.Validation.Formula1, ",")(0)
but it did not work well, it will only work if I put like "option1, option2" in the source in Data Validation window. If I refer the source to a range of options, then it will return with errors.
I guess there should some smarter ways.
Thanks!
On the worksheet where you applied the drop-down list, select a cell that has the drop-down list. Go to Data > Data Validation. On the Settings tab, click in the Source box, and then on the worksheet that has the entries for your drop-down list, Select cell contents in Excel containing those entries.
Create drop down lists in a UserForm, by using the ComboBox control. In this example, there are two ComboBox controls, one for Part ID, and one for Location.
Sub test()
Dim adr As String
With Range("c4")
adr = Mid(.Validation.Formula1, 2)
Debug.Print Range(adr).Cells(1, 1)
End With
End Sub
so your answer is:
set c = range("c4")
c.Value = Range(Mid(c.Validation.Formula1, 2)).Cells(1, 1).Value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With