Help to Optimize this vba code

jajamb

New Member
Joined
Apr 16, 2014
Messages
14
Hello.

Can anyone help me optimize this:

At the moment the range is from D6:D16, but if it gets longer, lets say D6:D100 this code is not so good.

So any ideas?





Code:
Sub Macro2()


Dim Filter(1 To 11) As String


Filter(1) = Sheets("Slicercellvalue").Range("D6").Value
Filter(2) = Sheets("Slicercellvalue").Range("D7").Value
Filter(3) = Sheets("Slicercellvalue").Range("D8").Value
Filter(4) = Sheets("Slicercellvalue").Range("D9").Value
Filter(5) = Sheets("Slicercellvalue").Range("D10").Value
Filter(6) = Sheets("Slicercellvalue").Range("D11").Value
Filter(7) = Sheets("Slicercellvalue").Range("D12").Value
Filter(8) = Sheets("Slicercellvalue").Range("D13").Value
Filter(9) = Sheets("Slicercellvalue").Range("D14").Value
Filter(10) = Sheets("Slicercellvalue").Range("D15").Value
Filter(11) = Sheets("Slicercellvalue").Range("D16").Value


    ActiveSheet.PivotTables("PivotTable2").PivotFields( _
        "[5Ansvar].[AnsvarKey].[AnsvarKey]").ClearAllFilters
        
    ActiveSheet.PivotTables("PivotTable2").PivotFields( _
        "[5Ansvar].[AnsvarKey].[AnsvarKey]").VisibleItemsList = Array( _
        "[5Ansvar].[AnsvarKey].&[" & Filter(1) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(2) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(3) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(4) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(5) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(6) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(7) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(8) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(9) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(10) & "]", _
        "[5Ansvar].[AnsvarKey].&[" & Filter(11) & "]")
End Sub
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,224,005
Messages
6,175,910
Members
452,682
Latest member
ghorne

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