Excel-VBA: Getting the values from Form Controls

2020-02-12 03:56发布

问题:

Embedded in the worksheet sheet1 I have a Form Control combo box named combobox_test and it has selected value 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_test and 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

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

回答1:

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.

Sub ButtonPressed_sample()

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

End Sub


回答2:

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


回答3:

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


回答4:

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"



回答5:

As said before the Shape ComboBox has no Value property.

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.

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

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