Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 1st, 2015, 02:51 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple Targets in one Worksheet

I wrote the below code, however:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim strField As String

strField = "Geography"

On Error Resume Next
Application.EnableEvents = False
Application.ScreenUpdating = False

    If Target.Address = Range("B18").Address Then
 

        Set ws = ThisWorkbook.Worksheets("DataPivotCategory")
            Set pt = ws.PivotTables("PivotTable3")
                With pt.PageFields(strField)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                            Exit For
                        Else
                            .CurrentPage = "(All)"
                        End If
                    Next pi
                End With
End If
    

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
I need to do multiple changes in this worksheet; i.e. i also need Cell B51 to be target value for a pivot item of a different pivot (per below). How do i do this if i can't have multiple target addresses in one module?

[code]
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim strField As String

strField = "Geography"

On Error Resume Next
Application.EnableEvents = False
Application.ScreenUpdating = False

If Target.Address = Range("B51").Address Then


Set ws = ThisWorkbook.Worksheets("DataPivotCategory")
Set pt = ws.PivotTables("PivotTable4")
With pt.PageFields(strField)
For Each pi In .PivotItems
If pi.Value = Target.Value Then
.CurrentPage = Target.Value
Exit For
Else
.CurrentPage = "(All)"
End If
Next pi
End With
End If


Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub
[code/]
  #2 (permalink)  
Old October 31st, 2015, 10:06 AM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Location: South Africa
Posts: 48
Thanks: 0
Thanked 5 Times in 5 Posts
Talking Multiple Targets in one Worksheet

Hi,

1. You are using the same code in both PivotTable3 & PivotTable4, after this: If Target.Address = Range("xxx").Address Then
Why not wrap this code in the function/Sub then call the same function twice, supplying the pivot table as a parameter for each call?

2. This is the same thing:
Code:
If Target.Address = Range("B18").Address Then
/
Code:
If Target.Address = Range("B51").Address Then
Select case Target.Address
Case "B18"
'Relevant Code
Case "B51"
'Relevant Code

Or ifThenElse.
If Target.Address = "B18" Then
'Relevant Code
ElseIf Target.Address = "B51"
'Relevant Code
Else
'Relevant Code
EndIf

On Worksheet Change the check the cell/Target that changed/triggered the Change event and execute relevant/unique steps applicable for that Target/TargetValue.

zweniryu@gmail.com
__________________
Nostalgia 4 Infinity


Similar Threads
Thread Thread Starter Forum Replies Last Post
set targets jofoxy2000 SQL Server 2008 0 April 21st, 2015 10:07 AM
Copying from worksheet to worksheet millenniumbuddy Excel VBA 5 August 24th, 2010 01:38 AM
Email Worksheet alannoble26 Excel VBA 2 November 7th, 2005 10:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.