Trying to clean up code that I did through a record macro

L

LabrGuy Bob R

Hello, Excel 2003 and I've recorded a macro to activate a worksheet and then
activate specific cells and format them with color, font, and alignment. The
code from the macro isquite large and I know wasteful. Could someone help me
with this to make it cleaner and do what I want? All of these cells are
located in a Pivot table that I have already established with code.

Sub ColorColumnHeaders()
Sheets("Rejected Voucher Statistics").Select
Columns("A:A").Select
Range("A3").Activate
Columns("A:A").EntireColumn.AutoFit -- autofitting the A column because
it didn't do it automatically
Range("A4").Select
With Selection
.HorizontalAlignment = xlCenter
End With
Range("B3").Select
ActiveSheet.PivotTables("PivotTable1").PivotSelect "Team[All]",
xlLabelOnly, _
True
Range("B4").Select
With Selection.Interior
.ColorIndex = 35 -- colors the cell in B4
.Pattern = xlSolid
End With
With Selection
.HorizontalAlignment = xlCenter ----- centers the cell in B4
End With
Selection.Font.Bold = True ------Bolds the font in B4
Range("A4").Select
ActiveSheet.PivotTables("PivotTable1").PivotSelect "SA[All]",
xlLabelOnly, True --- don't know what this does
Range("C4").Select
With Selection.Interior
.ColorIndex = 37 ((----- same coloring process above Column A, B
and C header for the pivor table are the only ones affected))
.Pattern = xlSolid
End With
With Selection
.HorizontalAlignment = xlCenter
End With
Selection.Interior.ColorIndex = 40
Range("D4").Select
With Selection.Interior
.ColorIndex = 40
.Pattern = xlSolid
End With
Range("D4").Select
Selection.Interior.ColorIndex = 10 ----- colors one cell at the top of
the count column
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'Chrl 1'",
xlDataAndLabel, _
True
Selection.Font.ColorIndex = 6
End Sub

Center Statistics
Center Team SA Total
\


Any help would be appreciated Thanks
BOB
 
J

JP

Hi Bob,

Try this instead:

Sub ColorColumnHeaders()

Set X = Range("A1, B1, C1") 'or whatever the cells you want to
operate on
X.Interior.ColorIndex = 3
X.HorizontalAlignment = xlCenter

' put some more code here

Set X = Nothing

End Sub


Hope this helps,
JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top