Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel macro (VB) - HELP! 1

Status
Not open for further replies.

TheBlade

MIS
Aug 12, 2001
29
0
0
NZ
I've got a spreadsheet with 10 pivot tables (each on separate sheets)...below is a macro for updating one pivottable and moving narrative underneath it pending on whether the table expands/contracts...how can i loop it so that the macro does it for each of the 10 pivottables and then stops (instead of having the macro below, but 10 times longer)...As you can tell, I don't much on VB, so ANY help would be very much appreciated. Thanks


Sub LeadsUpdate()

'Determine how much space the pivottable and leadsheet narrative needs
Columns("a:a").Select
Selection.find(What:="audit objectives", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False).Activate
ActiveCell.Select

'This is where the leadsheet narrative starts
Dim var As String
var = ActiveCell.Row

'This is where the leadsheet narrative should be
Dim var2 As String
var2 = Range("d5")
Dim var3 As String
var3 = Range("e5")

'Determine what should be moved first - pivot table or narrative
If var2 > var Then GoTo Option1 Else GoTo Option2:

Option1:
'Move narrative
ActiveCell.Rows("1:100").EntireRow.Select
Selection.Cut
Range(var3).Select
ActiveSheet.Paste
ActiveCell.Select

'Set pivottable category and refresh pivottable data
Dim var4 As String
var4 = Range("c5")
ActiveSheet.PivotTables("PivotTable4").PivotFields("Subject:").CurrentPage = _
var4
ActiveSheet.PivotTables("PivotTable4").RefreshTable
GoTo Finish:

Option2:
'Set pivottable category and refresh pivottable data
Dim var5 As String
var5 = Range("c5")
ActiveSheet.PivotTables("PivotTable4").PivotFields("Subject:").CurrentPage = _
var5
ActiveSheet.PivotTables("PivotTable4").RefreshTable

'Move narrative
ActiveCell.Rows("1:100").EntireRow.Select
Selection.Cut
Range(var3).Select
ActiveSheet.Paste
ActiveCell.Select


Finish:
Range("a1").Select
End Sub
 
Add this to the module, and change the "pivotx sheet names" to your real sheet names.

Then run your proceedure through this sub.

Sub Loop_all_pivots()

For i = 1 To Worksheets.Count

Select Case Worksheets(i).Name
Case Is = "pivot1 sheet name"
x = 1
Case Is = "pivot2 sheet name"
x = 1
Case Is = "pivot3 sheet name"
x = 1
Case Is = "pivot4 sheet name"
x = 1
Case Is = "pivot5 sheet name"
x = 1
Case Is = "pivot6 sheet name"
x = 1
Case Is = "pivot7 sheet name"
x = 1
Case Is = "pivot8 sheet name"
x = 1
Case Is = "pivot9 sheet name"
x = 1
Case Is = "pivot10 sheet name"
x = 1
Case Else
x = 0
End Select

If x = 1 Then LeadsUpdate

Next
End Sub
 
Here's a solution that is more general:
Code:
Sub UpdatePivotTables()
    For Each Worksheet In Application.Worksheets
        For Each PivotTable In Worksheet.PivotTables
            'do the stuff for your PivotTable
        Next
    Next
End Sub
Skip,
metzgsk@voughtaircraft.com
 
Nice code Skip....

I wasn't aware that the Worksheet.PivotTables propertie had a count.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top