Combine 2 codes in VBA

Dokat

Active Member
Joined
Jan 19, 2015
Messages
304
Office Version
  1. 365
Hi,

How can i combine below 2 codes? Also for lookup values i need to combine cells C4&F4. To do is this code makes sense?

Code:
If Intersect(Target, Target.Worksheet.Range("C4" & "F4")) Is Nothing Then Exit Sub


Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String


On Error Resume Next
If Intersect(Target, Target.Worksheet.Range("B4")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Source Data").PivotTables("PivotTable1")
Set xPFile = xPTable.PivotFields("Geography")
xStr = Target.Value
With xPFile
    .ClearAllFilters
    .PivotFilters.Add2 xlCaptionEquals, Value1:=xStr
End With
Application.ScreenUpdating = True


On Error Resume Next
If Intersect(Target, Target.Worksheet.Range("C4" & "F4")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Source Data").PivotTables("PivotTable1")
Set xPFile = xPTable.PivotFields("% Dollar Sales by Merch Any Price Reduction")
xStr = Target.Value
With xPFile
    .ClearAllFilters
    .PivotFilters.Add2 xlCaptionEquals, Value1:=xStr
End With
Application.ScreenUpdating = True


End Sub
 

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.
Seems like this would work..
Code:
If Intersect(Target, Target.Worksheet.Range("C4")) Is Nothing OR _
Intersect(Target, Target.Worksheet.Range("F4")) Is Nothing Then Exit Sub
HTH. Dave
 
Upvote 0
I used a different cell (A3) for to combine c4&f4. However when i ran second code doesnt work and filter the pivot table. What maybe causing this?

Code:
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String


On Error Resume Next
If Intersect(Target, Target.Worksheet.Range("B4")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Source Data").PivotTables("PivotTable1")
Set xPFile = xPTable.PivotFields("Geography")


xStr = Target.Value
With xPFile
    .ClearAllFilters
    .PivotFilters.Add2 xlCaptionEquals, Value1:=xStr
End With
Application.ScreenUpdating = True




If Intersect(Target, Target.Worksheet.Range("A3")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Source Data").PivotTables("PivotTable1")
Set xPFile1 = xPTable.PivotFields("% Dollar Sales by Merch Any Price Reduction")
xStr = Target.Value
With xPFile1
    .ClearAllFilters
    .PivotFilters.Add2 xlCaptionEquals, Value1:=xStr
End With
Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0
Not sure but U are using a worksheet change event... I hate them because if the code makes a sheet change then the code begins again before the initial code is actually completed. Not sure if that's happening here because I also hate filters and pivot tables and have no experience with their coding. Making A3 = C4 & F4 is not the same thing as doing something if either C4 or F4 is nothing. Anyways, trial just making a sub and running the code (ie. remove it from the ws change event) and see what happens. Dave
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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