adding two columns to vba userform combobox

℡╲_俬逩灬. 提交于 2019-12-25 07:23:37

问题


i have that userform for stock control which used to in or out items, all i want to do to amend the below code to show each item name beside it`s code to make searching items. Code:

Private Sub ComboBox1_Click()

Dim i As Integer
Dim j As Integer
Dim final As Integer
Dim FINAL2 As Integer

For i = 2 To 1000
    If Hoja5.Cells(i, 1) = "" Then
        final = i - 1
        Exit For
    End If
Next

For i = 2 To 1000
    If Hoja6.Cells(i, 1) = "" Then
        FINAL2 = i - 1
        Exit For
    End If
Next

For i = 2 To final
    If ComboBox1 = Hoja5.Cells(i, 1) Then
        TextBox1 = Hoja5.Cells(i, 2)
        Exit For
    End If
Next

For j = 1 To FINAL2
    If ComboBox1 = Hoja6.Cells(j, 1) Then
        TextBox8 = Hoja6.Cells(j, 3)
        Exit For
    End If
Next

End Sub


Private Sub ComboBox1_Enter()
Dim i As Integer
Dim j As Integer
Dim H As Integer
Dim final As Integer
Dim tareas As String

ComboBox1.BackColor = &H80000005

For i = 1 To ComboBox1.ListCount
    ComboBox1.RemoveItem 0
Next i

For j = 2 To 1000
    If Hoja5.Cells(j, 1) = "" Then
        final = j - 1
        Exit For
    End If
Next

For H = 2 To final
    tareas = Hoja5.Cells(H, 1)
    ComboBox1.AddItem (tareas)
Next
'End If

End Sub

photo indicating the required amendment

I want it to be like:

sample file

download sample for the above userform


回答1:


To do this:

  1. Set the ColumnCount = 2 for ComboBox

  2. Set the 2nd column value by ComboBox1.Column(1,{rowIndex}) = 'the value'`. In your code. it should be like:

    For i = 2 To final tareas = Hoja5.Cells(i, 1) ComboBox1.AddItem (tareas) '-- set the first column ComboBox1.Column(1, i - 2) = Hoja5.Cells(i, 2) 'the name Next




回答2:


You need to specify the ColumnCount value, and provide an array for the List property. If you want you can also specify a string containing a comma-separated list of column widths for the ColumnWidths property, too:

Option Explicit

Public Sub PopulateComboBox(ByVal source As Range, Optional ByVal valueColumn As Long = 1, Optional ByVal hasHeader As Boolean = True)
    With Me.ComboBox1
        .ColumnCount = source.Columns.Count
        .ColumnWidths = GetColumnWidths(source)
        .ListWidth = IIf(ComboBox1.Width > source.Width, ComboBox1.Width, source.Width)
        .List = source.Range(source.Rows(IIf(hasHeader, 2, 1)).EntireRow, source.Rows(source.Rows.Count).EntireRow).Value
        .BoundColumn = valueColumn
    End With
End Sub

Private Function GetColumnWidths(ByVal source As Range) As String
    Dim cols As Long
    cols = source.Columns.Count

    Dim widths()
    ReDim widths(1 To cols)
    Dim col As Long
    For col = 1 To cols
        widths(col) = source(, col).Width
    Next
    GetColumnWidths = Join(widths, ",")
End Function

Suppose you have a ListObject on a worksheet:

The code that works with the UserForm1 instance is responsible for calling the PopulateComboBox method:

Option Explicit

Sub Test()
    With New UserForm1
        .PopulateComboBox Sheet1.ListObjects(1).Range
        .Show vbModal
    End With
End Sub


Quite often, you want ComboBox.Text to be something user-friendly, and ComboBox.Value to be something useful - like some ID value:

The problem with doing that is that the ComboBox will always use the contents of the first column in its source for display, so you get this:

The solution is simply to hide the first column (i.e. set its width to 0):

Since valueColumn is 1, Me.ComboBox1.BoundColumn is referring to the hidden ID column, so when we do this:

Private Sub ComboBox1_AfterUpdate()
    If Not IsNull(ComboxBox1.Value) Then Debug.Print ComboBox1.Value, ComboBox1.Text
End Sub

This is what's printed in the immediate pane after selecting Lord of the Rings:

 1            Lord of the Rings

ComboBox1.Value is 1, and ComboBox1.Text is Lord of the Rings: now the rest of your code doesn't have to deal with string literals!




回答3:


This code will fill the combobox as well as adjust it's settings to display the your products. If you use it you will not be able to add or remove items from the combobox. You will have to edit the actual list on the worksheet.

  • .RowSource = "OFFSET(PRODUCT!$A$1,1,0,COUNTA(PRODUCT!$A:$A)-1,3)" Links the combobox's list to the a dynamic list on the worksheet. It will skrink and grow with that list.
  • .ColumnCount = 3 Sets the Combobox to display 3 Columns
  • .ColumnWidths = "100pt;100pt;200pt" Sets the width of each column
  • .ListWidth = "400pt" Sets the width of the drop down. Which I made bigger then the actual combobox.


Private Sub UserForm_Initialize()

    With ComboBox1
        .RowSource = "OFFSET(PRODUCT!$A$1,1,0,COUNTA(PRODUCT!$A:$A)-1,3)"
        .ColumnCount = 3
        .ColumnWidths = "100pt;100pt;200pt"
        .ListWidth = "400pt"
    End With

End Sub


来源:https://stackoverflow.com/questions/39422101/adding-two-columns-to-vba-userform-combobox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!