(ByVal Target As Range) Not Working

MB161

Board Regular
Joined
May 8, 2014
Messages
69
Afternoon,

I am trying to get the filter on a pivot to change to the wholesaler selected from a drop down menu on another sheet. (There are some slight format changes between the list of wholesalers the selection is made from and their labelling in the raw data which the pivot accesses, hence the lookup). The code below works when ran manually but I wanted to automate the process by using (ByVal Target As Range). I think I understand where it is going wrong but I'm not sure which changes I need to make to get it working.

Any help would be greatly appreciated!

Sub PivotChange(ByVal Target As Range)


Table1 = Sheets("Acrynyms").Range("D2:F24")

On Error Resume Next
Result = Application.WorksheetFunction.VLookup(Sheets("Returns Note").Range("B8"), Table1, 3, False)
If Err <> 0 Then
Result = xlErrNA
End If


If Not Application.Intersect(Target, Sheets("Returns Note").Range("B8")) Is Nothing Then
Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name"). _
ClearAllFilters
Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name").CurrentPage _
= Result
End If


End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
A few things.

You have not declared "Table1". If it is a range object (which it appears to be), you need to use the "Set" statement, like you did with "KeyCells".

"Target" is already a range object. So "Range(Target.Address)" is totally unnecessary. Just use "Target".
 
Upvote 0
I have made the changes you suggested and feel like I'm getting close to the solution but the filter is still not updating. Any ideas what I'm missing?

Code:
Sub Worksheet_Change(ByVal Target As Range)


    Dim KeyCells As Range
    Dim Table1 As Range
    
    Set KeyCells = Sheets("Returns Note").Range("B8")
    Set Table1 = Sheets("Acrynyms").Range("D2:F24")
 
 On Error Resume Next
    Result = Application.WorksheetFunction.VLookup(Sheets("Returns Note").Range("B8"), Table1, 3, False)
    If Err <> 0 Then
        Result = xlErrNA
    End If


 If Not Application.Intersect(KeyCells, Target) Is Nothing Then
 Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name"). _
    ClearAllFilters
    
    Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name").CurrentPage _
    = Result
    
 End If
 
Upvote 0
What happens if you remove On Error Resume Next and re-arrange things a bit.
Code:
Sub Worksheet_Change(ByVal Target As Range)
Dim Result As Variant
Dim KeyCells As Range
Dim Table1 As Range
    
    Set KeyCells = Sheets("Returns Note").Range("B8")
    Set Table1 = Sheets("Acrynyms").Range("D2:F24")
 
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then

        Result = Application.VLookup(Target, Table1, 3, False)

        If IsError(Result) Then Exit Sub

        Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name").ClearAllFilters
        Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name").CurrentPage = Result

    End If

End Sub
By the way, where have you put the code?
 
Upvote 0
I have written the code in "This Worksheet" part of the VBA editor. Was this the right place to do so?

I have left the office now and won't be able to let you know if your code works until tomorrow morning when I have the sheet in front of me again.

In the meantime, thanks for your help so far. I look forward to finding the solution!
 
Upvote 0
There is no ThisWorksheet in the VBA editor, if you mean ThisWorkbook then that's not the correct place for the code.

It should go in a worksheet module.
 
Upvote 0
I have written the code in "This Worksheet" part of the <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> editor.
I don't think any such object exists. There is a "ThisWorkbook" module, and sheet modules named like "Sheet1 (Sheet1)".
Worksheet event procedure code needs to be placed in the Sheet modules.

An easy way to get it in the correct place is to go to the sheet you want to apply it to in Excel, right-click on the sheet tab name at the bottom of the screen, select "View Code" and paste your code in the VB Editor window that pops up.
 
Upvote 0
I'd have to check that's where I put it in the morning but I guess that would explain why nothing is happening. I'll let you know. Thanks
 
Upvote 0
This worked perfectly thanks although not when added as a module but when pasted into the correct sheet as an object via right clicking the the tab and selecting "View Code" as Joe4 said. I'm not sure I fully understand the difference between an object and a module but will read up on this.

A big thank you to you both for your help!

What happens if you remove On Error Resume Next and re-arrange things a bit.
Code:
Sub Worksheet_Change(ByVal Target As Range)
Dim Result As Variant
Dim KeyCells As Range
Dim Table1 As Range
    
    Set KeyCells = Sheets("Returns Note").Range("B8")
    Set Table1 = Sheets("Acrynyms").Range("D2:F24")
 
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then

        Result = Application.VLookup(Target, Table1, 3, False)

        If IsError(Result) Then Exit Sub

        Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name").ClearAllFilters
        Sheets("Pivot").PivotTables("PivotTable1").PivotFields("Agent Name").CurrentPage = Result

    End If

End Sub
By the way, where have you put the code?
 
Upvote 0
You are not actually adding an object, but you are adding it to a pre-defined module (there are Workbook and Sheet modules). If you add Event Procedure VBA code to a pre-defined module, they are triggered automatically upon some event happening.

You are probably used to adding your VBA code to General VBA modules. That is most of the code that you run manually, or by a button or shortcut. Event Procedure VBA code cannot be added to a General Module (it won't run automatically).

Here is a good write-up on Event Procedures: Events In Excel VBA
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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