Excel-VBA:从表单控件获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4219769/
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-08 10:56:33  来源:igfitidea点击:

Excel-VBA: Getting the values from Form Controls

vbauser-controlsexcel-vbaexcel

提问by Carlos

Embedded in the worksheet sheet1I have a Form Control combo box named combobox_testand it has selected value x

嵌入在工作表中,sheet1我有一个名为 Form Control 的组合框combobox_test,它已选择值x

in addition to that, i also have embbeded a button that when i press it i want it to take the value of combobox_testand place it in something.Rows(y). But i cant get it working and am getting a bit frustrated. Maybe you can point me in the right direction

除此之外,我还嵌入了一个按钮,当我按下它时,我希望它取值combobox_test并将其放入something.Rows(y). 但我不能让它工作,我有点沮丧。也许你能指出我正确的方向

Sub ButtonPressed_sample()
    Dim value As String

    Set putItRng = Range("theCells")        
    putItRng.Rows(1) = ActiveSheet.Shapes("combobox_test").Value        
End Sub

Any advise? Am an absolute beginner in VBA, so please be as detailed as you can. Thanks

有什么建议吗?我是 VBA 的绝对初学者,所以请尽可能详细。谢谢

采纳答案by Dick Kusleika

I'm not sure this is what you want, but it's a start. The Shape object doesn't have a Value property, which is the source of the error. There is a DropDown object that is deprecated, but still available.

我不确定这是你想要的,但这是一个开始。Shape 对象没有 Value 属性,这是错误的来源。有一个 DropDown 对象已被弃用,但仍然可用。

Sub ButtonPressed_sample()

    Set putitrng = Range("theCells")
    putitrng.Rows(1) = ActiveSheet.DropDowns("combobox_test").value

End Sub

回答by Paul

   Sub QuickSelect_Change()
        With ActiveSheet.Shapes("QuickBox")
            MsgBox "My Selected Value " & .ControlFormat.List(.ControlFormat.ListIndex)
        End With
    End Sub

回答by rdg515

ActiveSheet.Shapes("combobox_test").ControlFormat.ListIndex

回答by Stiebl

As said before the Shape ComboBox has no Value property.

如前所述,Shape ComboBox 没有 Value 属性。

I use the DrawingObject property of the Shape object to get a CheckBox FormControl object. This CheckBox object can then be used like any other FormControl.

我使用 Shape 对象的 DrawingObject 属性来获取 CheckBox FormControl 对象。然后可以像使用任何其他 FormControl 一样使用此 CheckBox 对象。

You should also be able to use the DrawinObject to get the ComboBox objcet form the Shape object.

您还应该能够使用 DrawinObject 从 Shape 对象中获取 ComboBox 对象。

If you want to get te selected text then you can try following code snipped:

如果您想获得选定的文本,则可以尝试剪切以下代码:

Dim sh as Shape
Dim cB as ComboBox
For Each sh In ws.Shapes
    If sh.Type = msoFormControl Then
        If TypeOf sh.DrawingObject Is ComboBox Then
            Set cB = sh.DrawingObject
            ... 
            your code for getting the Data from ComboBox
            ...
        End If
    End If
Next

回答by Patrick

putItRng.Rows(1)= ActiveSheet.combobox_test.value

Try:

尝试:

activesheet.cells(1,putItRng.column).value=activesheet.combobox_test.value

If it doesnt work then your combobox is not named "Combobox_test"

如果它不起作用,则您的组合框未命名为“Combobox_test”