macro to clear fill colour

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a pivot table an need to clear the fill colour in Col D from 1 row below where Grand total appears in Col A


I have written code to do this, but fill colour is not cleared

Kindly amend my code

Code:
  Sub Clear_Colors()
  Dim rng As Range
Dim Lastrow As Long
Application.ScreenUpdating = False
    Sheets(1).Select
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
        For i = 4 To Lastrow
            Set rng = Range(Cells(i, 1), Cells(i, 4))
            If InStr(Cells(i, 1).Value, "Grand") Then
            With rng.Offset(1).Resize(15.5)
            
               With .Interior.Pattern = xlNone
                     End With
                      
                        End With
                    
                    
                    Else
                With rng
                    With .Interior
                        .Pattern = xlNone
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                    With .Font
                        .ColorIndex = xlAutomatic
                        .TintAndShade = 0
                    End With
                     With rng
                     .Font.Bold = False
                       End With
                 
                End With
            End If
        Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try something like this instead:

Code:
Sub Clear_Colors()
 Dim PT as pivottable
Dim Lastrow As Long

    set pt = Sheets(1).Pivottables(1)
    Lastrow = pt.tablerange1.row + pt.tablerange1.rows.count
    cells(lastrow, 1).resize(16).Interior.Pattern = xlNone

End Sub

I wasn't really sure what trying to resize the range to 15.5 rows was meant to do?
 
Upvote 0
Thanks for the help. The resize should have been 15,5

Thanks for the help Rory


I have amended your code

Code:
 cells(lastrow, 1).resize(16).Interior.Pattern = xlNone


To 

cells(lastrow, 1).resize(16,5).Interior.Pattern = xlNone

Code works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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