2
This other one just below the same post of the link above .. has difference in running ? ... https://stackoverflow.com/a/43139189/9948374
There is a way to speed up the execution of some of these codes. Binary format type ... or change n same code.. ?
I put a button to execute. because with.
Private Sub Worksheet_selectionchange(Byval Target As Range)
As soon as I put the first letter it already starts to run .. this leaves the spreadsheet "heavy" ... would like if not with a button were to give enter in cell "C18" . (At the end of each word searched).
@danieltakeshi, I would like when deleting the cell.. ("C18") = Empty .. to appear in the place "Search by address here".. execute Clearallfilters... in case the cell ("C18") whenever there is no search to do will have "Search by address here".
but the filter has behaved this way with the code... selects the last filter item with text searches that do not have among the options.
It’s like this..
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
'https://stackoverflow.com/questions/42929493/filter-items-with-certain-text-in-a-pivot-table-using-vba
Dim PvtTbl As PivotTable
Dim PvtItm As PivotItem
Dim f As String
On Error GoTo Sair
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
If Target.Address = "$C$18" Then
f = Target.Value
' set the pivot table
Set PvtTbl = PivotTables("Tabela dinâmica9")
PvtTbl.ManualUpdate = True
With PvtTbl.PivotFields("Conteúdo variável 5")
.ClearAllFilters
For Each PvtItm In .PivotItems
If PvtItm.Name Like "*" & f & "*" Then
PvtItm.Visible = True
Else
PvtItm.Visible = False
End If
Next PvtItm
End With
End If
Sair:
Set PvtTbl = PivotTables("Tabela dinâmica9")
PvtTbl.ManualUpdate = False
Debug.Print Err.Number
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Private Sub TextBox1_Change()
textoDigitado = Range("$C$18").Text
Call PreencheLista
End Sub
Private Sub UserForm_Initialize()
'Ao iniciar o formulario ira chamar o procedimento PreencheLista
Call PreencheLista
End Sub
Private Sub PreencheLista()
textoDigitado = TextBox1.Text
'código que irá filtrar os nomes
Dim linha As Integer
Dim TextoCelula As String
linha = 1
'limpa os dados do formulário
ListBox1.Clear
'Irá executar até o último nome
While ActiveSheet.Cells(linha, 1).Value <> Empty
'pega o nome atual
TextoCelula = ActiveSheet.Cells(linha, 1).Value
'quebra a palavra atual pela esquerda conforme a quantidade de letras digitadas e compara com o texto digitado
If InStr(UCase(TextoCelula), UCase(textoDigitado)) > 0 Then
'se a comparação for igual será adicionado no formulario
ListBox1.AddItem ActiveSheet.Cells(linha, 1)
End If
linha = linha + 1
Wend
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("C18").Value = Empty Then
Range("C18").Value = "Faça a busca por endereço aqui"
Call ClearReportFiltering
Else
End If
End Sub
Clearreportfiltering is .. Macro to . Clearallfilter in Fileds in TD (That’s the way I found it for now)
Also add your code as it is, which you’ve tried?
– Costamilam
Sheets("Dashboard")
is the name of the Spreadsheet the table is in,PivotTables("PivotTable1")
is the table name ,PivotFields("TLEG")
table field name,Sheets("Dashboard").Range("Lane1").Value
cell with value to be searched– danieltakeshi
You are placing the code on the worksheet where you want the event to be activated? As explained in this reply. For the event
Worksheet_Change
is being used.– danieltakeshi
After a lot of research ... This code does exactly what I need to ... https://stackoverflow.com/questions/42929493/filter-items-with-certain-text-in-a-pivot-table-using-vba. I want to change the lines.... f = Inputbox("Type the text you want to filter:") If Not pi.Name Like "" & f & "" Then. I want to replace it with a specific cell ($C$18) ... To search filter items.
– André Machado
@Andrémachado See Edit in the part of the Worksheet_change Event
– danieltakeshi
@danieltakeshi .. I would like to thank you very much for your collaboration in completing this project. I’m months into it, and I had to put it aside for a while ... but after your suggestions it works excellently. I’m a VBA beginner ... These codes I showed you were the closest I could find on the network. I know it’s heavy code, but any new suggestion from you to improve it.. and expedite your purpose of Find filters ... will be very welcome. Once again I thank you very much for your time spent helping me.
– André Machado
@Andrémachado Dynamic Tables are slow, I would recommend the use of Vectors (Arrays), which is more complex, or Autofilter normal table. You can also optimize for the spreadsheet not to perform automatic calculations, only when all changes are made, you activate the calculations only once manually. There are tutorials of how to accomplish this on the internet. Besides, to optimize it is necessary a more advanced understanding of VBA and the same application. With studies is possible or you can post in Codereview. But read that before
– danieltakeshi
@danieltakeshi.. I edited the post .. When applying the search for an item that is not in the filter.. always pull the last filter item. I put it at the top of the code. Option Compare Text .. this solves the difference between uppercase and minuscule ? What can be done in the words with accents.. André has an accent .. but if you look for Andre without an accent the search does not find ? Last thought I had was... this code ...
– André Machado
@When a value that does not exist is entered, the error
Não é possível definir a propriedade Visible da classe PivotItem
appears, then the last item in which the error occurred will be shown, not necessarily the last item. And the word must contain something exact using the Wilcard withIf PvtItm.Name Like "*" & f & "*" Then
, if you want something different try using Regex or see more information about the operatorLike
orWildcards
. Or a function can be written to compare the String letter by letter and ignore accents.– danieltakeshi
@danieltakeshi.. Private Sub Preencher()... It is very efficient in searching the column a .... I imagined taking the search results that are in Listbox ... (active Multselect option) and selecting in Listbox among the options that appeared which I want. OU... Take all results one by one and apply them to the TD filter... Possible ... What do you think ?
– André Machado
@Andrémachado This question is getting too extensive, I suggest asking another question about the possibility of Listbox. Always use divide-and-conquer tactics to solve programming problems.
– danieltakeshi
@danieltakeshi .. https://answall.com/q/308932/116347.. new topic.
– André Machado