【问题标题】:Value retrieval from the Multiple selection dropdown in excel从 excel 中的多项选择下拉列表中检索值
【发布时间】:2019-03-28 13:29:34
【问题描述】:

我正在尝试从我的下拉菜单中进行计算。 这就是我的下拉菜单的样子。 类别

  • AAA
  • BBB
  • CCC
  • DDD

这些是与我的下拉菜单关联的值。 类别类别值

  • AAA 1
  • 血脑屏障 2
  • CCC 3
  • DDD 4

我为多选添加了 VBA 代码,还添加了简单的 Vlookup 公式来检索类别的值。

=VLOOKUP(E2;Sheet2!I2:J5;2;)

使用 VBA 代码,我可以选择所有三个类别并稍后删除所选类别。但我无法检索所选类别的总和。例如我希望如果客户选择类别 AAA 和 CCC,他/她应该能够看到总和为 4。此外,如果客户首先选择所有三个类别,然后删除其中一个类别,那么总和也应该得到更新。我不知道如何更新我的 Vlookup 公式以获得总和。

这是我的多选 VBA 代码。

Private Sub Worksheet_Change(ByVal Target As Range)
    'Updated: 2016/4/12
    Dim xRng As Range
    Dim xValue1 As String
    Dim xValue2 As String
    If Target.Count > 1 Then Exit Sub
    On Error Resume Next
    Set xRng = Cells.SpecialCells(xlCellTypeAllValidation)
    If xRng Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If Not Application.Intersect(Target, xRng) Is Nothing Then
        xValue2 = Target.Value
        Application.Undo
        xValue1 = Target.Value
        Target.Value = xValue2
        If xValue1 <> "" Then
            If xValue2 <> "" Then
                '                If xValue1 = xValue2 Or _
                '                   InStr(1, xValue1, ", " & xValue2) Or _
                InStr(1, xValue1, xValue2 & ",") Then
                If InStr(1, xValue1, xValue2 & ",") > 0 Then
                    xValue1 = Replace(xValue1, xValue2 & ", ", "") ' If it's in the middle with comma
                    Target.Value = xValue1
                    GoTo jumpOut
                End If
                If InStr(1, xValue1, ", " & xValue2) > 0 Then
                    xValue1 = Replace(xValue1, ", " & xValue2, "") ' If it's at the end with a comma in front of it
                    Target.Value = xValue1
                    GoTo jumpOut
                End If
                If xValue1 = xValue2 Then        ' If it is the only item in string
                    xValue1 = ""
                    Target.Value = xValue1
                    GoTo jumpOut
                End If
                Target.Value = xValue1 & ", " & xValue2
            End If
            jumpOut:
        End If
    End If
    Application.EnableEvents = True
End Sub

【问题讨论】:

  • 请注意:看看使用If.....then....elseif...else....end ifSelect case 这样你就可以摆脱GoTo jumpOut
  • 我应该用 VBA 来代替 Vlookup 编码?

标签: excel vba dropdown


【解决方案1】:

您无需在 VBA 中编码,只需使用此函数即可。

=SUMPRODUCT(ISNUMBER(SEARCH(Sheet2!A1:I4;Sheet1!A2))*Sheet2!B1:B4)

【讨论】:

    【解决方案2】:

    我会为求和部分做类似的事情。我有我的参考表 A1:B4。我打电话给Get_Sum("A,C,D")

    Function Get_Sum(strInput As String) As Double
    
    Dim a() As String
    Dim v As Variant
    Dim r As Excel.Range
    Dim l As Long
    
    a = Split(strInput, ",")
    Set r = Range("a1:b4")
    
    Get_Sum = 0
    
    For Each v In a
        l = Application.WorksheetFunction.Match(v, r.Columns(1), 0)
        Get_Sum = Get_Sum + r.Cells(l, 2)
    Next v
    
    Set r = Nothing
    Erase a
    
    End Function
    

    这样叫

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        '   Where A5 is the validated cell and B5 is the sum result
    
        If Target = Range("a5") Then
            Range("b5").value = Get_Sum (Target.Value)
        End If
    
    
        End Sub
    

    【讨论】:

    • 使用此代码,我的多选选项不起作用。
    • 6 分钟后 :o) 你怎么称呼它,我看不到你的机器。就像我在帖子中添加的那样,当你有Get_Sum("AAA,BBB,CCC")?您是否将参考表更改为您的数据所在的位置?
    • 我正在尝试在 if 语句中添加这个函数
    • Get_Sum (''AAA,BBB,CCC') 仅在客户从下拉菜单中选择 AAA、BBB、CCC 时才可用。我为下拉菜单关联了一个值,在 Vlookup 的帮助下,我能够获得关联的值。但是当我尝试进行多选时,我的 VBA 代码不会计算与所选菜单关联的值的总和。
    • 在您的IF...Intersect 语句中,您可以删除\注释掉您的代码。调用此函数并发送组合框中所有选择的字符串。需要注意的另一件事是,从您的代码中删除 On Error Resume Next,因为它只是隐藏了问题
    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    相关资源
    最近更新 更多