Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open form with combobox item selected

Tags:

excel

vba

I’m loading a 2 column table into a 2 column combobox, column 2 is the bound column. I want to have the form open with a combobox item already selected. I’m having success with Macro1, but in Macro2 the line .cmb1.Value = 4 raises an error. I’m not sure why that is, could someone give me an explanation?

enter image description here

Sub Macro1()
    Dim f As myFrm
    Set f = New myFrm
    With f
        .cmb1.RowSource = [myLst].Address
        .cmb1.Value = 4 'displays Wed in combo
    End With

    f.Show

End Sub

Sub Macro2()
    Dim f As myFrm, v
    Set f = New myFrm
    With f
        .cmb1.List = [myLst].Value
        .cmb1.Value = 4 'Error
    End With

    f.Show

End Sub
like image 330
DaveU Avatar asked Dec 05 '25 10:12

DaveU


1 Answers

Good question. Here's the relevant part of Excel VBA 2010 help:

For a ComboBox, changing the contents of Value does not change the value of BoundColumn.

Not that you need it, but here's an alternative:

With f.cmb1
    .List = [myLst].Value
    For i = 0 To .ListCount - 1
        If .List(i, 1) = 4 Then
            .ListIndex = i
            Exit For
        End If
    Next i
End With
like image 147
Doug Glancy Avatar answered Dec 08 '25 02:12

Doug Glancy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!