Small Part of VBA Pivot Table code Needs Fix

boldcode

Active Member
Joined
Mar 12, 2010
Messages
347
Hi Everyone,

I am trying to fix my VBA code, but I can't seem to find out what is wrong with it. The second part of code should change the cell background color all cells associated with the value "N8F", but for some reason the color stops on cell A72 when it should stop at A127.

Here is the first part of my code which works fine:

Code:
Sub FormatHeaderRowDos()
    Dim c As Range
    Dim PT As PivotTable
    Dim PivotRange As Range
    
    Set PT = ActiveSheet.PivotTables(1)
    Set PivotRange = PT.DataBodyRange
    
    With ActiveSheet.PivotTables(1)
        
        With .TableRange1
            .Interior.ColorIndex = 0
            .Font.Bold = False
        End With
        
        With Intersect(.PivotFields("FiscalYear").DataRange.EntireRow, _
            .TableRange1)
            .Font.Bold = True
            .Font.ColorIndex = 35
            .Interior.ColorIndex = 56
        End With
    End With

Here is the second part of code which does not work like it suppose to:

Code:
Dim cell As Range
    Dim ColourIndex As Long
    Dim LastRow As Long

    Set PT = ActiveSheet.PivotTables(1)
    Set PivotRange = PT.DataBodyRange
    
    LastRow = Range("B" & Rows.Count).End(xlUp).Row

    For Each cell In Range("A10:A" & LastRow)
        Select Case cell.Value
            Case "N8F"
                ColourIndex = RGB(235, 241, 222)
            Case Is = "N45"
                ColourIndex = RGB(235, 241, 222)
        End Select
        cell.Interior.Color = ColourIndex
    Next
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi BoldCode,

Is your PT in Tabular, Compact or Outline Layout?

In your code, the last row processed in Col A is based on the last value in Col B - does the data in Col B extend to B127?
If so, it's hard to see why the processing would stop at A72.

Not sure if this is related, but you might want to add Cases for blank cells or Cells with values other than N8F or N45 to be explicit about what you want to happen when those 2 cases are not true.

Rich (BB code):
Select Case cell.Value
    Case "N8F"
        ColourIndex = RGB(235, 241, 222)
    Case "N45"
        ColourIndex = RGB(235, 241, 222)
    Case ""
        'for Tabular or Outline format keep same as Parent label    
    Case Else
        'Parent label changed to value other than N8F or N45
        ColourIndex = RGB(255, 255, 255)
End Select

You could also add:
Rich (BB code):
if Cell.Row > 70 then Debug.Print Cell.Row & ":" & Cell.Value
inside your For each...Next loop to see if what is happening around Row 72
 
Upvote 0
JS411,

Your are right about the following:

"In your code, the last row processed in Col A is based on the last value in Col B - does the data in Col B extend to B127?
If so, it's hard to see why the processing would stop at A72."

The data does not extend to B127, the minute I changed from Col B to Col D the rest of the cells in column A were the right color.

How can I fix the code so that regardless of column B the Cells associated with the value "N8F" even though they are blank will be colored the same as "N8F".
 
Upvote 0
The data does not extend to B127, the minute I changed from Col B to Col D the rest of the cells in column A were the right color.

Good- that explains that part.

Will you post a small screen shot of part of your PivotTable?

The approach might be a little different depending on whether or not you have a Compact Layout (since two fields share the same column).

It will also help to see how you are handling subtotals.
 
Upvote 0
JS411,

My pivot table is in Tabular format:

<table style="width: 1147px; height: 405px;" border="0" cellpadding="0" cellspacing="0"><col style="width:48pt" width="64"> <col style="mso-width-source:userset;mso-width-alt:4352;width:89pt" width="119"> <col style="mso-width-source:userset;mso-width-alt:2742;width:56pt" width="75"> <col style="mso-width-source:userset;mso-width-alt:3766;width:77pt" width="103"> <col style="mso-width-source:userset;mso-width-alt:2742; width:56pt" span="2" width="75"> <col style="mso-width-source:userset;mso-width-alt:2706;width:56pt" width="74"> <col style="mso-width-source:userset;mso-width-alt:2779;width:57pt" width="76"> <col style="mso-width-source:userset;mso-width-alt:2742;width:56pt" width="75"> <tbody><tr style="height:13.5pt" height="18"> <td style="height:13.5pt;width:48pt" height="18" width="64">
</td> <td class="xl67" style="width:89pt" width="119">COLUMN A</td> <td class="xl67" style="width:56pt" width="75">COLUMN B</td> <td class="xl67" style="width:77pt" width="103">COLUMN C</td> <td class="xl67" style="width:56pt" width="75">COLUMN D</td> <td class="xl67" style="width:56pt" width="75">COLUMN E</td> <td class="xl67" style="width:56pt" width="74">COLUMN F</td> <td class="xl67" style="width:57pt" width="76">COLUMN G</td> <td class="xl67" style="width:56pt" width="75">COLUMN H</td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 9</td> <td class="xl68">SNIP</td> <td class="xl68">Type</td> <td class="xl68">Name</td> <td class="xl68">Model</td> <td class="xl68">2011</td> <td class="xl69">2012</td> <td class="xl69">2013</td> <td class="xl70">2014</td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 10</td> <td class="xl71">N8F</td> <td class="xl71">N/A</td> <td class="xl71">PETRA</td> <td class="xl71">JCS</td> <td class="xl71"> </td> <td class="xl72">890</td> <td class="xl72"> </td> <td class="xl73"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 11</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl75">PKL</td> <td class="xl75"> </td> <td class="xl67">876</td> <td class="xl67">890</td> <td class="xl76"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 12</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl75">NMB</td> <td class="xl75">600</td> <td class="xl67">
</td> <td class="xl67">
</td> <td class="xl76"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 13</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl71">CONSIGNMENT</td> <td class="xl71">GH</td> <td class="xl71">700</td> <td class="xl72"> </td> <td class="xl72"> </td> <td class="xl73"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 14</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl71">MATERIALS</td> <td class="xl71">GH</td> <td class="xl71">900</td> <td class="xl72"> </td> <td class="xl72"> </td> <td class="xl73"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 15</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl71">RECLAIM</td> <td class="xl71">GH</td> <td class="xl71">800</td> <td class="xl72"> </td> <td class="xl72"> </td> <td class="xl73"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 16</td> <td class="xl74"> </td> <td class="xl71">JAG</td> <td class="xl71">PURCHASE</td> <td class="xl71">YU</td> <td class="xl71">890</td> <td class="xl72"> </td> <td class="xl72"> </td> <td class="xl73"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 17</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl75">PO</td> <td class="xl75">780</td> <td class="xl67">
</td> <td class="xl67">
</td> <td class="xl76"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 18</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl75">TI</td> <td class="xl75">657</td> <td class="xl67">
</td> <td class="xl67">
</td> <td class="xl76"> </td> </tr> <tr style="height:12.75pt" height="17"> <td class="xl66" style="height:12.75pt" height="17">ROW 19</td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl74"> </td> <td class="xl75">MIT</td> <td class="xl75">890</td> <td class="xl67">
</td> <td class="xl67">
</td> <td class="xl76"> </td> </tr> </tbody></table>
 
Upvote 0
Sorry, I don't know how to post as Screen Shot, but I tried to give you a visual of what the table looks like.
 
Upvote 0
The tabular format makes this easier. One way to approach this is the code I showed previously...
Code:
Select Case cell.Value
    Case "N8F"
        ColourIndex = RGB(235, 241, 222)
    Case "N45"
        ColourIndex = RGB(235, 241, 222)
    Case ""
        'for Tabular or Outline format keep same as Parent label    
    Case Else
        'Parent label changed to value other than N8F or N45
        'Reset to whatever your default color is for "no fill" cells
        ColourIndex = RGB(255, 255, 255)
End Select

Are you trying to add fill color to all the columns associated with N8F, or just Column A?
Also- do you have subtotals- that would need it's own case.

I probably won't able to get back to this until tonight...hopefully this will get you pointed in the right direction, or perhaps another helper can continue this thread.
 
Upvote 0
JS411,

Are you trying to add fill color to all the columns associated with N8F, or just Column A?

Just Column A

Also- do you have subtotals- that would need it's own case.

I do have subtotals

Thanks for your help. No worries if you can't get back until tonight.
 
Upvote 0
Here is some code that you should be able to adapt.

It uses .TableRange1 instead of fixed references to Col A, Col D and Row 9which should allow you to change the postition of the PT or the number of Label fields without having to edit your code.

Rich (BB code):
Sub PT_Color()
    Dim cell As Range, rngPTLabels As Range
    Dim ColourIndex As Long
    
    Set rngPTLabels = ActiveSheet _
        .PivotTables(1).TableRange1
    
    For Each cell In rngPTLabels.Resize(, 1)
        Select Case cell.Value
            Case "N8F"
                ColourIndex = RGB(235, 241, 222)
            Case "N45"
                ColourIndex = RGB(235, 241, 222)
            Case ""
                'for Tabular or Outline format keep same as Parent label  
          Case Else
                'Parent label changed to value other than N8F or N45
                If Right(cell.Value, 5) = "Total" Then 'SubTotal
                    ColourIndex = -4142 'xlNone
                Else  'All Other labels
                    ColourIndex = -4142 'xlNone
                End If
        End Select
        cell.Interior.Color = ColourIndex
    Next
End Sub

Below is the result for an example intended to show the switch between no color to color to no color. You can revise the code if you want the subtotal row for N8F and N45 to match the value rows.

Excel Workbook
ABCDE
9SNIPTypeNameModel2011
10A99N/AMATERIALSJCS530
11NMB200
12MATERIALS Total730
13PETRAGH520
14JCS10
15PKL260
16PETRA Total790
17N/A Total1520
18A99 Total1520
19N45JAGMATERIALSPKL90
20MATERIALS Total90
21JAG Total90
22N/AMATERIALSGH180
23JCS280
24NMB270
25MATERIALS Total730
26PETRAJCS40
27NMB30
28PKL90
29PETRA Total160
30N/A Total890
31N45 Total980
32Z11JAGMATERIALSJCS300
33NMB190
34PKL330
Sheet1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top