سؤال


I have a combobox working as listbox in my userform. Combobox is set to display 2 columns value but when one of the listbox values is chosen only value from column 1 is shown.

Is it possible to show two values - just as 2 columns values work in listbox?

Question asked due to my work with initialization of userform. When it's initialized it should show specific data, but if it's gonna show me only 1 column value most of the time it's useless.

Private Sub UserForm_Initialize()

zmienna = Klient_kraj.Controls("klient").ListIndex

us.ColumnCount = 2
us.List = Range("us").Value

If Not Klient_kraj.Controls("klient").Value = "" Then
    nip.Value = Range("klienci").Cells(1 + zmienna, 1).Offset(0, 1).Value

    'problem starts here
    us.Value = Range("klienci").Cells(1 + zmienna, 1).Offset(0, 3).Value 
End If

End Sub
هل كانت مفيدة؟

المحلول

Just set the column count of the combo box to 2. I don't really see what the problem is.

Edit:

Here is a possible workaround. Make the combobox wide enought to display the necessary values in the two columns. Place a label control over the combobox in the position that the second column would be, but make sure that the right hand side of the label control does not overlap the drop down arrow of the combo box. Set the background style property of the label to transparent. Place the following code in the combo box's change event:

Private Sub ComboBox1_Change()

    Me.Label1.Caption = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1)

End Sub

نصائح أخرى

Another workaround but without the need of a Label:

Private Sub ComboBox1_Change()
    With Me.ComboBox1
        'The if is there to control the unwanted trigger of this code
        If .Value <> "" And InStr(.Value, vbTab) = 0 Then
            .Value = .Value & vbTab & .Column(1)
        End If
    End With
End Sub
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top