Excel VBA组合框的值与文本不同

时间:2017-08-18 12:52:48

标签: excel vba excel-vba combobox excel-2013

我正在设置一个组合框,在选中时会更新数据透视表。但我需要组合框返回与所选文本不同的值。

离。您可以在下拉框“Cheerios”中选择产品名称。但它的SKU编号为1234.我需要组合框才能返回1234,而不是“Cheerios”这个词。

这可能吗?

编辑:

下面是我填充列表的图片。 B列是下拉列表中显示的内容,A列是我选择产品时需要返回的内容。

enter image description here

编辑2:

Private Sub cmb_SkuSelect_Click()
    Dim xlSheetSort As Worksheet
    Dim lastRow As Long
    Dim skuValue As Integer

    Set xlSheetSort = ActiveWorkbook.Worksheets("Sort")
    lastRow = xlSheetSort.Range("A1").End(xlDown).Row

    With xlSheetSort.Range("B1:B" & lastRow)
        Set c = .Find(cmb_SkuSelect.Value, LookIn:=xlValues)
        If Not c Is Nothing Then
            skuValue = xlSheetSort.Range("A" & c.Row).Value
        End If
    End With

    cmb_SkuSelect.Value = ""
    ActiveWorkbook.ActiveSheet.Range("A4").Value = skuValue

    updatePivot skuValue
 End Sub

updatePivot:

Public Sub updatePivot(ByVal sku As Integer)
    Dim xlSheet As Worksheet
    Dim xlPTable As PivotTable

    Set xlSheet = ActiveWorkbook.Worksheets("Sku Inventory")

    For Each xlPTable In xlSheet.PivotTables
        With xlPTable
            .PivotFields("Sku Number").CurrentPage = sku
        End With
    Next
End Sub

2 个答案:

答案 0 :(得分:1)

试试:

Private Sub ComboBox1_Change()
    Dim valueToLook As String
    valueToLook = ComboBox1.Value
    Dim sku, i As Integer
    Dim LastRow As Long
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With
    For i = 1 To LastRow
        If Cells(i, 2).Value = valueToLook Then
            sku = Cells(i, 1).Value
            MsgBox sku
            Exit For
        End If
    Next i
End Sub

答案 1 :(得分:0)

是的,绝对有可能-您需要做的就是将组合框设置为多列,将其BoundColumn设置为SKU编号的列,然后将其隐藏。这个答案很晚,但是可以解决您的问题。

我不会打扰您的现有结构,而是提供一个简单的示例,您以后可以适应您的需求。假设您已经在标准工作簿中拥有表单(例如UserForm1)和组合框(例如ComboBox1),请将其粘贴到您的userform模块中:

Private Sub ComboBox1_Change()
    ' Show the expected result to the user
    MsgBox Me.ComboBox1.Value
End Sub

Private Sub UserForm_Initialize()
  Dim cbitems(2, 1) As Variant
    ' Set the number of columns in the combobox to 2 (i.e. Product Name and SKU Number)
    Me.ComboBox1.ColumnCount = 2
    ' Set the 2-nd column's value to be used as the value of the combobox (i.e. the SKU Number)
    Me.ComboBox1.BoundColumn = 2
    ' Set the 2-nd column's width to 0, hiding the column (i.e. only the Product Name is visible)
    Me.ComboBox1.ColumnWidths = ";0"
    ' Populate a 2D array with the values of the combobox columns
    cbitems(0, 0) = "Cheerios"
    cbitems(0, 1) = "1234"
    cbitems(1, 0) = "Apples"
    cbitems(1, 1) = "1672"
    cbitems(2, 0) = "Peaches"
    cbitems(2, 1) = "3722"
    ' Populate the combobox with the above array, using the List method (i.e. not the AddItem one)
    Me.ComboBox1.List = cbitems
    ' Set the 1-st item of the combobox to be its default one
    Me.ComboBox1.ListIndex = 0
End Sub

这些评论解释了会发生什么。您所要做的就是使代码适应您的使用情况(例如,您可能希望使用循环来填充数组/组合框,也许将相关代码移至与用户表单的Initialize事件不同的Sub上,如果您希望两列都可见,请添加列标题。

注意:当第一次在用户窗体上绘制组合框时,组合框的Change事件将触发几次,因此一开始可能会出现一些空的消息框。不用管它们,重要的是稍后您从列表中选择某些项目之后的那些。无论如何,消息框只是为了方便起见,它与用户窗体的Initialize事件的最后一行一起,一旦您想到了它们,就可以安全地将它们从代码中删除。