Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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/]
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:27 PM.


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