Imagine que voce tenha uma planilha cheia de dados e gostaria de filtrar uma tabela dinamica com um dado especifico para visualizar um grafico dinamico dessa informacao.
Utilize este codigo abaixo, e tenha um filtro rapido e lindo de visualizar
Chame a procedure a partir do evento BeforeDoubleClick da planilha que o usuario vai clicar 2x sobre a celula do valor que ele quer filtrar
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' se for coluna B, diferente de Vazio e numero de linha maior q 7 chama a funcao
If Target.Column = 2 And Target.Value <> "" And Target.Row > 7 Then 'B
Call filterpvtEmployee(Target.Value)
End If
End Sub
Codigo do modulo
Sub filterpvtEmployee(pname As String)
'
' filterpvtEmployee Macro
' runs this macro when double click a name in pivot table for the month
'
'
Application.ScreenUpdating = False
Sheets("Trend YTD").Select
'ActiveSheet.PivotTables("pvttrendO").PivotFields("Name").ClearAllFilters
'Speeds up code dramatically
ActiveSheet.PivotTables("pvttrendO").ManualUpdate = True
i = 1
With ActiveSheet.PivotTables("pvttrendO").PivotFields("Name")
'exibe o que for certo
ActiveSheet.PivotTables("pvttrendO").PivotFields("Name").PivotItems(pname).Visible = True
'esconde o q nao eh pra mostrar
For Each itm In .PivotItems
Application.StatusBar = "Filtering data: " & Str(Round((i / .PivotItems.Count) * 100, 0)) & " % complete"
If itm.Value <> pname Then
itm.Visible = False
End If
i = i + 1
Next itm
End With
'Speeds up code dramatically
ActiveSheet.PivotTables("pvttrendO").ManualUpdate = False
Application.ScreenUpdating = True
End Sub
Obrigado pela dica.
ResponderExcluirComecei a pouco tempo minhas aulas de excel com macro pelo curso adv, www.cursoadv.com.br
Estou gostando muito da ferramenta de macros.