Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default update pivot fields VBA

I may have answered my own question, or at least troubleshot, didn't figure
out what's actually happening...

Anyway, I copied the pivot table that was working and pasted over the one
that wasn't, then updated the fields to look the way I wanted the cover
sheet.

I went around in so many circles on Friday, I swore I tried that already.

But it worked. So disregard my post...

-Liver

"Justin Larson" wrote:

I'm having a strange problem, and am hoping someone here can enlighten me a
bit. As pretext, I have NO background in VBA, so any advice must come in the
form of a-monkey-could-be-taught language.

I have a workbook with several sheets. On each of these sheets is one or
more Pivot tables. I have setup VBA code so that when a set of control cells
is updated, all page fields in all of the pivot tables changes to the new
value from the control cell. So the user chooses a date and a state from a
menu, and all the various reports throughout the workbook should show data
for that state/date.

For the purposes of testing I have set up the control cells to be B1 and C1.
After I get the code working, I will change them to something hidden and use
form controls to update the cells (they're prettier).

The code reads that if the value entered can't be found, it defaults to
(All) which exists in every pivot table.

In any case, I have gotten to the point that when you change either item, it
updates both page fields on every pivot table in the workbook, which is what
it should do. The problem is that it can never find the date value for the
first pivot table. No matter what date value I put into the control cell,
that first table goes to (All). The rest of them are updating correctly. All
the pivot tables are drawing from the same source.

Before you ask, this code is primarily borrowed from someone else's, I did
not write it from scatch, just cut/paste/modified to meet needs.

Here's the code, any ideas, you VBA geniuses?

Option Explicit


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 = "State"

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

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

For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
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
Next pt
Next ws
End If

strField = "Sale_Date"

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

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

For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
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
Next pt
Next ws

End If

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

-Liver

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
fields names do not show in excel 2007 pivot table fields list marlo17 New Users to Excel 2 December 1st 08 01:25 PM
Pivot Data fields - removal of all (value) fields Bradley Searle[_2_] Excel Programming 2 November 13th 08 02:09 PM
number of fields in the row fields in pivot table UT Excel Discussion (Misc queries) 0 April 13th 06 01:17 AM
Update fields? Cannabibal Excel Programming 2 December 7th 04 10:49 AM
Update fields with blanks kash Excel Discussion (Misc queries) 1 December 3rd 04 09:18 PM


All times are GMT +1. The time now is 06:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"