2017-12-05 9 views
1

"Main"시트에 피벗 테이블이 있습니다. PivotField "Report Filter"에는 200 개국이 포함 된 "국가 별 코드"가 있습니다. InputBox를 사용하여 해당 필터에서 1 개 이상의 국가를 표시하고 싶습니다.Excel VBA에서 여러 PivotItem을 표시하는 방법

문제는 필터에서 수동으로 하나 이상의 국가 또는 ALL을 선택하고이 프로그램을 실행해야한다는 것입니다. 이렇게하면 올바른 데이터를 가져올 수 없습니다. 모든 국가의 선택을 취소하고 실행해야합니다.

내 코드

Sub Addcountries() 

Dim ws As Worksheet 
Dim str1 As Variant 
Dim Data As Variant 
Dim pf As PivotField 
Dim target As PivotTable 

Set ws = Sheets("Main") 
str1 = Application.InputBox("Enter the Country - comma separated") 

If str1 = False Then 
    MsgBox "Please Enter one Country", , "Filter Country" 
    Exit Sub 
Else 
    If InStr(1, str1, ",") > 0 Then 
     Data = Split(str1, ",") 
     For i = LBound(Data) To UBound(Data) 
      ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(Data(i)).Visible = True 
     Next i 
    Else 
     ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(str1).Visible = True 
    End If 
End If 

End Sub  
+0

잘 모르겠습니다.이 코드에 오류가 있습니까? 또는 기능이 누락 되었습니까? –

+0

문제는 그가 Inputbox에서 사용하고있는 데이터와 관련이 있다고 생각합니다. 그리고 '피벗 테이블 (Pivot Table)'은 수동 입력이 아닌 Excel의 실제 참조 값을 사용합니다. @ 디팍 : 입력 항목에 Excel을 사용해 보았습니까? –

+0

@shim 오류가 발생하지 않습니다. 이 코드를 개선해야합니다. 필터의 모든 항목을 제거해야합니다. – Deepak

답변

1

당신 수있는 PivotItems 컬렉션을 루프하고 Data 배열 내부의 선택 COUNTIRES 중 하나와 일치하는 경우 각 PivotItem.Name을 확인 - 당신이 수행 할 수있는 그 Match 기능을 사용.

코드 당신이 당신에게 진정한 가치를 볼뿐만 아니라하지 않을 피벗 항목에 거짓 표시 값을 줄 필요가 작동하도록 필터 위해

If str1 = False Then 
    MsgBox "Please Enter one Country", , "Filter Country" 
    Exit Sub 
Else 
    If InStr(1, str1, ",") > 0 Then ' more than 1 country >> create array 
     Data = Split(str1, ",") 
    Else ' single country 
     Data = Array(str1) '<-- create array with 1 element (for Match to work) 
    End If 

    ' === You need a different loop, loop through all Pivot-Items, then look for a match with Data (array) === 
    Dim pi As PivotItem 

    ' clear previous Filter 
    ws.PivotTables("MainTable").PivotFields("Country Code").ClearAllFilters 

    For Each pi In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems 
     ' check if current Pivot-Item equals one of the elements of the array (use Match function) 
     If Not IsError(Application.Match(pi.Name, Data, 0)) Then ' Match successful 
      pi.Visible = True 
     Else 
      pi.Visible = False 
     End If 
    Next pi 
End If 

' rest of your code 
+0

코드가 거의 작동하고 있습니다. 2 작은 문제. 1) 내가 1 개국을 선택했을 때 작동하지 않고 계속 ALL을 선택합니다. 2, 프로그램을 두 번째로 실행하면 필터를 지울 필요가 있습니다. 그 일을하는 방법 – Deepak

+0

@Deepak 나는 당신의 게시물에 따라 코드를 썼다. 나는 두 번째 옵션도 수정할 것이다. –

+0

@Deepak 지금 편집 된 코드 –

0

하나의 이렇게 모든 피벗 항목을 참 또는 거짓으로 설정했는지 확인하십시오.

다음은 아이디어를 제공하는 코드입니다.

Sub Addcountries() 
    Dim ws As Worksheet 
    Dim str1 As Variant 
    Dim Data() As Variant 
    Dim pf As PivotField 
    Dim target As PivotTable 
    Dim PivotItem As Object 
    Dim ShowMe As Boolean 


    Set ws = Sheets("Main") 
    str1 = Application.InputBox("Enter the Country - comma separated") 

    If str1 = False Then 
     MsgBox "Please Enter one Country", , "Filter Country" 
     Exit Sub 
    Else 
     If InStr(1, str1, ",") > 0 Then 
      Data = Split(str1, ",") 
     Else 
      'Make Single Item Array 
      ReDim Data(1) 
      Data(0) = str1 
     End If 
     For Each PivotItem In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems 
      'Default Visibility Is False 
      ShowMe = False 
      For i = LBound(Data) To UBound(Data) 
       'Loop Through Each Item In Data To See If You Should ShowMe 
       ShowMe = (PivotItem.Name = Data(i)) 
       If ShowMe Then 
        'Quit Early If You ShowMe 
        Exit For 
       End If 
      Next i 
      PivotItem.Visible = ShowMe 
     Next PivotItem 
    End If 

End Sub