Excel の複数選択ドロップダウンからの値取得

Excel の複数選択ドロップダウンからの値取得

ドロップダウン メニューから計算を行おうとしています。Excel のシート 1 に次のドロップダウンがあります。

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

シート 2 には、このドロップダウンに対応する値があります。

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

複数選択用の VBA コードを追加し、VLOOKUPカテゴリの値を取得するための簡単な数式も追加しました。

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

VBA コードを使用すると、3 つのカテゴリすべてを選択し、後で選択したカテゴリを削除することもできます。ただし、選択したカテゴリの合計を取得できません。たとえば、顧客がカテゴリ AAA と CCC を選択した場合、合計は 4 と表示されるはずです。また、顧客が最初に 3 つのカテゴリすべてを選択し、そのうちの 1 つを削除した場合、合計が更新されるはずです。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

答え1

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

これはうまくいくはずだが、通常の方式ではない。配列数式。配列数式を機能させるには、ないではなく、 ++のEnter組み合わせを使用します。CtrlShiftEnter

また、をA1実際のドロップダウン セルに変更します。

例

関連情報