Recuperação de valor do menu suspenso de seleção múltipla no Excel

Recuperação de valor do menu suspenso de seleção múltipla no Excel

Estou tentando fazer cálculos no meu menu suspenso. Eu tenho o seguinte menu suspenso na planilha 1 do meu Excel.

## Category ##
### AAA ###
### BBB ###
### CCC ###
### DDD ###

Na planilha 2, tenho valores correspondentes para este menu suspenso.

## Category  Category Value##
### AAA    1###
### BBB    2###
### CCC    3###
### DDD    4###

Adicionei código VBA para seleção múltipla e também adicionei VLOOKUPuma fórmula simples para recuperar o valor da categoria.

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

Com o código VBA, posso selecionar todas as três categorias e também remover a categoria selecionada posteriormente. Mas não estou conseguindo recuperar a soma da categoria selecionada. Por exemplo, se um cliente escolher a categoria AAA e CCC, ele poderá ver a soma como 4. Além disso, se um cliente escolher primeiro todas as três categorias e depois remover uma delas, a soma deverá ser atualizada. Não estou entendendo como atualizo minha VLOOKUPfórmula para obter a soma.

Aqui está meu código VBA para seleção múltipla.

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

Responder1

=SUM(IF(ISERR(FIND(Sheet2!$I$2:$I$5;A1;1));0;Sheet2!$J$2:$J$5))

Isto deve funcionar, mas não é uma fórmula regular. É umvariedadeFórmula. Para que as fórmulas de matriz funcionem, você as inserenãocom Enter, mas em vez disso use uma combinação de Ctrl+ Shift+ Enter.

Além disso, altere A1para sua célula suspensa real.

Exemplo

informação relacionada