Hello All,
Is there a way to simplify below various macro into one to generate faster output.
Currently it is taking a longer time.
Below macro is filtering various pivots and finally copying the data linked to all pivot to another workbook named "Fare".
Is there a way to simplify below various macro into one to generate faster output.
Currently it is taking a longer time.
Below macro is filtering various pivots and finally copying the data linked to all pivot to another workbook named "Fare".
Code:
.[Merged].[Merged]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT1_POS()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT1")
filtvalues = .Range("A1:A2").Value
End With
Set PF = pt.PivotFields("[DDS].[Sales POS].[Sales POS]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Sales POS]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Sales POS]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT1_Cabin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT1")
filtvalues = .Range("C1:C2").Value
End With
Set PF = pt.PivotFields("[DDS].[Cabin].[Cabin]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Cabin]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Cabin]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT2_OD()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT2")
filtvalues = .Range("B1:B2").Value
End With
Set PF = pt.PivotFields("[DDS].[Merged].[Merged]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT2_POS()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT2")
filtvalues = .Range("A1:A2").Value
End With
Set PF = pt.PivotFields("[DDS].[Sales POS].[Sales POS]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Sales POS]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Sales POS]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT2_Cabin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT2")
filtvalues = .Range("C1:C2").Value
End With
Set PF = pt.PivotFields("[DDS].[Cabin].[Cabin]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Cabin]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Cabin]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT3_OD()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT3")
filtvalues = .Range("B1:B2").Value
End With
Set PF = pt.PivotFields("[DDS].[Merged].[Merged]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT3_POS()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT3")
filtvalues = .Range("A1:A2").Value
End With
Set PF = pt.PivotFields("[DDS].[Sales POS].[Sales POS]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Sales POS]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Sales POS]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT3_Cabin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT3")
filtvalues = .Range("C1:C2").Value
End With
Set PF = pt.PivotFields("[DDS].[Cabin].[Cabin]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Cabin]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Cabin]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT4_OD()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT4")
filtvalues = .Range("B1:B2").Value
End With
Set PF = pt.PivotFields("[QSI].[OD].[OD]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT5_OD()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT5")
filtvalues = .Range("B1:B2").Value
End With
Set PF = pt.PivotFields("[QSI].[OD].[OD]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT6_OD()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT6")
filtvalues = .Range("B1:B2").Value
End With
Set PF = pt.PivotFields("[QSI].[OD].[OD]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT7_Cabin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT7")
filtvalues = .Range("C1:C2").Value
End With
Set PF = pt.PivotFields("[Bkd FRCT].[Cabin Class].[Cabin Class]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Cabin Class]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Cabin Class]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "Cabin Class_P7"
End If
End Sub
Sub PT7_Origin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT7")
filtvalues = .Range("BE2:BE3").Value
End With
Set PF = pt.PivotFields("[Bkd FRCT].[Sector].[Sector]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Sector]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Sector]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT8_Cabin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT8")
filtvalues = .Range("C1:C2").Value
End With
Set PF = pt.PivotFields("[Bkd FRCT].[Cabin Class].[Cabin Class]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Cabin Class]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Cabin Class]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "Cabin Class_P8"
End If
End Sub
Sub PT8_Destination()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT8")
filtvalues = .Range("BF2:BF3").Value
End With
Set PF = pt.PivotFields("[Bkd FRCT].[Sector].[Sector]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Sector]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Sector]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT9_OD()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT9")
filtvalues = .Range("BI1:BI2").Value
End With
Set PF = pt.PivotFields("[POS_OD].[OD].[OD]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[OD]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub PT9_ODCabin()
Dim filtvalues As Variant, aItm As Variant
Dim pt As PivotTable
Dim PF As PivotField
Dim tmpFltr As String, strFltr As String, arrFltr As Variant
Dim aItmFlts As String
With Sheets("Pivot")
Set pt = .PivotTables("PT9")
filtvalues = .Range("BG1:BG2").Value
End With
Set PF = pt.PivotFields("[POS_OD].[COMPARTMENT_CODE].[COMPARTMENT_CODE]")
PF.ClearAllFilters
For Each aItm In filtvalues
aItmFlts = "[" & aItm & "]"
On Error Resume Next
tmpFltr = WorksheetFunction.Substitute(PF.Name, "[COMPARTMENT_CODE]", "&" & aItmFlts, 2)
PF.VisibleItemsList = Array(tmpFltr)
If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[COMPARTMENT_CODE]", "&" & aItmFlts, 2)
On Error GoTo 0
Next aItm
If strFltr <> "" Then
arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
PF.VisibleItemsList = arrFltr
Else
MsgBox "None of the values exist"
End If
End Sub
Sub SheetCopy()
On Error Resume Next
Cells.Select
Range("B3").Activate
Selection.Copy
Windows("Fare.xlsx").Activate
Sheets.Add after:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Name = Range("B4").Value
On Error GoTo 0
End Sub
]