Simulate ComboBox selection from another workbook

PoggiPJ

Active Member
Joined
Mar 25, 2008
Messages
330
I would like to simulate a user selection in a ComboBox in WB2 from a macro that is running in WB1.

The name of the worksheet in WB2 that contains the ComboBox is "Inputs"
The ComboBox.name in WB2 is "inputPlanRateCountry"

In WB2 macros I can read the selected ComboBox value using (for example) CountrySelected = Inputs.inputPlanRateCountry.Value

But if when I try to assign something to the ComboBox .value or .text property from a statement in WB1, I encounter a "Method or data member not found" error.

Following is code from the macro in Workbook1....
Code:
Set WB2 = Workbook2.Worksheets("Inputs")

WB2.inputPlanRateCountry.Value = WB1.Range("custSelectedPlanRate").Value
Is there any way that I can enter a country value into that combo box, just as though a user had actually clicked/selected it?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Not yet - Is that a property that I can only read, or can I set it from the calling macro?
 
Upvote 0
This syntax worked for me
Code:
With Workbooks("Book2").Sheets(1).OLEObjects(2).Object
    .Value = "a"
End With
 
Upvote 0
Try this funcion :

Code:
Private Function SelectListBoxItem _
(ByVal ListBox As Object, Item As Variant) As Boolean

    Dim ListItems()
    Dim lIndex As Long
    
    ReDim ListItems(ListBox.Object.ListCount)
    ListItems = ListBox.Object.List
    On Error Resume Next
        lIndex = WorksheetFunction.Match(Item, ListItems, 0)
    On Error GoTo 0
    If lIndex <> 0 Then
        ListBox.Object.Value = Item
        SelectListBoxItem = True
    End If

End Function
Usage :

Code:
Sub Test()
    
    Set WB2 = Workbook2.Worksheets("Inputs")
    
    If Not SelectListBoxItem(WB2.OLEObjects("inputPlanRateCountry").Value, _
    WB1.Range("custSelectedPlanRate").Value) Then
        MsgBox "Value not found in the list."
    End If

End Sub
 
Upvote 0
Gentlemen, thank you both. This was my first experience with OLEObjects and placing the statement in context like that helped tremendously.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top