I am fairly new to VBA and I am trying to get better. I have a workbook that I am trying to loop a code through. but only on specific worksheets - not the whole book. Basically, I wrote a code that will reformat a pivot table as a table, and format the headers etc. This works perfectly in one sheet. But I have 10 more sheets this needs to be done with - for my colleagues (who is not excel wizards, it would be better if this could be done with the touch of a button - so to say).
I have googled for hours and tried many different stuff, sometimes I don't get an error but the code isn't applied to the other worksheets when running as macro.
(B11 is a static starting point for all sheets)
This is the code:
Sub Ultimo_Pivot_Table()
'Start Loop?
'Select and copy pivot
Columns("B:O").Select
Selection.Copy
Columns("P:P").Select
'Paste pivot in new area
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Delete old pivot
Columns("B:O").Select
Range("O1").Activate
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
'Select & Format as table
With Range("B11")
.Parent.ListObjects.Add(xlSrcRange, Range(.End(xlDown), .End(xlToRight)), , xlYes).Name = "Table1"
End With
'Format Headlines
With Range("B11")
Range(Selection, Selection.End(xlToRight)).Select
End With
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = -0.499984740745262
End With
Range("B2").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'End Loop?
End Sub
I have tried so many different things so this is the raw code without any attempts at looping. Any suggestions? Thank you!