Can anyone look at this code and improve on it? I am somewhat new to programming. It takes along time for it to complete. I think it slows down where my If statements are.
Thanks
Rick
Sub FilterPivotCustomerAddress()
'
Dim C_Address As String
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim ws As Worksheet
Application.ScreenUpdating = False
On Error Resume Next
C_Address = Sheets("Address Search").Range("CustomerAddress").Value
Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PivotFields("Sales Order")
Set pf1 = pt.PivotFields("Street")
pt.ClearAllFilters
If C_Address <> " " Then
For Each pi In pf.PivotItems
Dim leftPi As String
leftPi = Left(pi.Value, 1)
If leftPi <> "H" Then
pi.Visible = False
End If
If pi.Visible = False And leftPi = "R" Then
pi.Visible = True
End If
If pi.Visible = False And leftPi = "S" Then
pi.Visible = True
End If
Next pi
pf1.PivotFilters.Add _
Type:=xlCaptionBeginsWith, Value1:=C_Address
Else
MsgBox ("Please enter a Value in Cell D1 ")
End If
pt.ManualUpdate = False
Set pf = Nothing
Set pt = Nothing
For Each ws In ThisWorkbook.Worksheets
ws.Columns.AutoFit
Next ws
Range("D1").Select
End Sub
Thanks
Rick
Sub FilterPivotCustomerAddress()
'
Dim C_Address As String
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim ws As Worksheet
Application.ScreenUpdating = False
On Error Resume Next
C_Address = Sheets("Address Search").Range("CustomerAddress").Value
Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PivotFields("Sales Order")
Set pf1 = pt.PivotFields("Street")
pt.ClearAllFilters
If C_Address <> " " Then
For Each pi In pf.PivotItems
Dim leftPi As String
leftPi = Left(pi.Value, 1)
If leftPi <> "H" Then
pi.Visible = False
End If
If pi.Visible = False And leftPi = "R" Then
pi.Visible = True
End If
If pi.Visible = False And leftPi = "S" Then
pi.Visible = True
End If
Next pi
pf1.PivotFilters.Add _
Type:=xlCaptionBeginsWith, Value1:=C_Address
Else
MsgBox ("Please enter a Value in Cell D1 ")
End If
pt.ManualUpdate = False
Set pf = Nothing
Set pt = Nothing
For Each ws In ThisWorkbook.Worksheets
ws.Columns.AutoFit
Next ws
Range("D1").Select
End Sub