Excel-VBA: Getting the values from Form Controls
Asked Answered
S

6

12

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

Shippen answered 18/11, 2010 at 21:29 Comment(1)
Run-time error 438. Object doesn't support this property or method... and it shows the line putItRng.Rows(1) = ActiveSheet.Shapes("combobox_test").ValueShippen
B
8

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
Bipartisan answered 19/11, 2010 at 17:51 Comment(1)
Note, DropDowns (and ComboBoxes, Buttons, etc) are deprecated because if you choose to group several shapes together, controls that are in groups don't appear in those collection objects. So I guess this comment is just a caution not to put your control into any grouped shapes so long as you're using this method (which I myself also use) :)Premarital
B
11
   Sub QuickSelect_Change()
        With ActiveSheet.Shapes("QuickBox")
            MsgBox "My Selected Value " & .ControlFormat.List(.ControlFormat.ListIndex)
        End With
    End Sub
Boatel answered 15/12, 2010 at 15:7 Comment(1)
Thank you. Perfect!Acrid
B
8

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
Bipartisan answered 19/11, 2010 at 17:51 Comment(1)
Note, DropDowns (and ComboBoxes, Buttons, etc) are deprecated because if you choose to group several shapes together, controls that are in groups don't appear in those collection objects. So I guess this comment is just a caution not to put your control into any grouped shapes so long as you're using this method (which I myself also use) :)Premarital
B
3
ActiveSheet.Shapes("combobox_test").ControlFormat.ListIndex
Booty answered 10/1, 2014 at 13:25 Comment(3)
While this can answer the question, it would be better to explain why it is the right solution.Pleochroism
The highest voted answer references the DropDowns collection, but Alain mentioned that this was deprecated because "if you choose to group several shapes together, controls that are in groups don't appear in those collection objects". The shapes collection is another means to reference a combobox form control, but Carlos post didn't work for me.Booty
I just tried ActiveSheet.Shapes("combobox_test").ControlFormat.Value and found it got the same result. Some scenarios might find this to be a more appropriate coding style.Shadbush
B
1
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"

Bohon answered 18/11, 2010 at 21:59 Comment(1)
You must reference by ActiveWorkbook.Activesheet ('cause multiple workbooks each have one Activesheet). Rows(1) gives you the entire range of all columns in row 1... Go step by step on this. Try find the real source of the problem using two Msgbox, one for the left side, the other for the right side of the above suggested assignment. That will better pinpoint what's the matter. I'm guessing your object naming is wrong.Cardinale
A
1

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
Alon answered 17/10, 2018 at 21:22 Comment(0)
S
0

Thanks been fighting with this this one but this topic gave me an answer. I don't know a difference of method1 or method2 reference type, value can be read using .value|.List attributes. It would be great to have a fully typed obj variable.

Dim obj As Object
Set obj = ws.DropDowns("combo1")  ' method 1
Set obj = ws.Shapes("combo1").ControlFormat  ' method 2
Debug.Print obj.value & "|" & obj.List(obj.value)  ' 1...n|Text1,Text2,..n
    
Storekeeper answered 2/12, 2020 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.