Dynamic table - pivotitem

finaljustice

Board Regular
Joined
Oct 6, 2010
Messages
175
Hello, i've never used a dynamic table and a collegue of mine asked me to work on a workbook of his. I noticed he would have to do some repetitive things and decided to make a macro (making our jobs easier).
Basically its a code that copies the a template worksheet named 300000 and copies and renames the sheet following a list. Creating the sheets and renaming them I was able to do.
Code:
Sub centrocustosheets()
Application.ScreenUpdating = False
Sheets(2).Select
Range("A3").Select
contar = Range(ActiveCell, ActiveCell.End(xlDown)).Count
ActiveCell.Offset(1, 0).Select
Do While ActiveCell <> Empty
    nomesheet = ActiveCell.Text
    Sheets("300000").Select
    Sheets("300000").Copy After:=Sheets(7)
    Sheets("300000 (2)").Select
    Sheets("300000 (2)").Name = nomesheet
 
    Sheets(2).Select
    ActiveCell.Offset(1, 0).Select
 
Loop
Application.ScreenUpdating = True
End Sub


Now inside the sheets there are 3 dynamic tabels, where i have to "apply a filter" with this new sheets name. I tried recording the macro and got this. (its portuguese but "TABELA DINÂMICA1" means "Dynamic Table1"; "CENTRO DE CUSTOS" means "Costs Centers").

Code:
ActiveSheet.PivotTables("Tabela dinâmica1").PivotFields("Centro de Custos"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Tabela dinâmica1").PivotFields("Centro de Custos" _
        )
        .PivotItems("300000").Visible = False
        .PivotItems("301000").Visible = True 'here is where I am having a problem, I wish to change this to the variable name of the sheet "nomesheet" but I keep getting an error.
    End With

Any ideas how I can change this? I will always be making the
.PivotItems("300000").Visible = False
.PivotItems("nomesheet").Visible = True (applying the new "filter" to the same cost center as the newly generated sheet.

I would expect the end code to look something like this ( I just don't know the correct way to code it):
Code:
Sub centrocustosheets()
Application.ScreenUpdating = False
Sheets(2).Select
Range("A3").Select
contar = Range(ActiveCell, ActiveCell.End(xlDown)).Count
ActiveCell.Offset(1, 0).Select
Do While ActiveCell <> Empty
    nomesheet = ActiveCell.Text
    Sheets("300000").Select
    Sheets("300000").Copy After:=Sheets(7)
    Sheets("300000 (2)").Select
    Sheets("300000 (2)").Name = nomesheet
ActiveSheet.PivotTables("Tabela dinâmica1").PivotFields("Centro de Custos"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Tabela dinâmica1").PivotFields("Centro de Custos" _
        )
        .PivotItems("300000").Visible = False
        .PivotItems("nomesheet").Visible = True     
End With
ActiveSheet.PivotTables("Tabela dinâmica1").PivotFields("Centro de Custos"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Tabela dinâmica2").PivotFields("Centro de Custos" _
        )
        .PivotItems("300000").Visible = False
        .PivotItems("nomesheet").Visible = True     
End With
ActiveSheet.PivotTables("Tabela dinâmica1").PivotFields("Centro de Custos"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Tabela dinâmica3").PivotFields("Centro de Custos" _
        )
        .PivotItems("300000").Visible = False
        .PivotItems("nomesheet").Visible = True     
End With
 
    Sheets(2).Select
    ActiveCell.Offset(1, 0).Select
 
Loop
Application.ScreenUpdating = True
End Sub


Thank your for your attention and help.
Your sincerely,
final
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,224,583
Messages
6,179,676
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top