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 코드를 사용하면 세 가지 범주를 모두 선택하고 나중에 선택한 범주를 제거할 수도 있습니다. 하지만 선택한 카테고리의 합계를 검색하지 못했습니다. 예를 들어, 고객이 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

답변1

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

이것은 작동해야 하지만 일반적인 공식은 아닙니다. 이것은정렬공식. 배열 수식이 작동하려면 다음을 입력하세요.~ 아니다with 를 사용 Enter하는 대신 Ctrl+ Shift+ 의 조합을 사용하세요 Enter.

또한 A1실제 드롭다운 셀로 변경하세요.

예

관련 정보