如何使用 VBA 在 Excel 的下拉列表中选择第一个 iterm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18126636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:38:16  来源:igfitidea点击:

How to use VBA to select the first iterm in a drop down list in Excel

excelvba

提问by Xiao Qiang

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?

我使用数据验证来创建一些下拉列表。任何人都知道如何使用 VBA 在下拉列表中选择第一个 iterm?

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.

但它效果不佳,只有当我在“数据验证”窗口的源中输入“option1,option2”时,它才会起作用。如果我将源引用到一系列选项,那么它将返回错误。

I guess there should some smarter ways.

我想应该有一些更聪明的方法。

Thanks!

谢谢!

采纳答案by Patrick Honorez

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

set c = range("c4")
c.Value = Range(Mid(c.Validation.Formula1, 2)).Cells(1, 1).Value