macro to color items

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,592
Office Version
  1. 2021
Platform
  1. Windows
I have a macro which I have written to color A3:E3 with an accent style as well as where "Grand total" is found + 4 columns after Grand total is found


The macro colors A3:E3 + E4 and does not color the rows where"Grand Total is found" + 4 columns after this for eg if Grand Total is in A64, then A64:D64 to be colored using accent style


Code:
  Sub Colour_GrandTotal()
Sheets("pivot table").Select
With Range("A3:E3")
.Style = "Accent5"
End With



    Dim r As Range
    
    Set r = Sheets("Pivot Table").Cells.Find(What:="*Grand Total*", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)
    If Not r Is Nothing Then r.Resize(, 4).Style = "Accent5"
    Sheets("Pivot Table").Select
    
    
 End Sub


it would be appreciated if someone could assist me
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
When you step though the code what does r equal? Sounds like it is not finding grand total.
 
Upvote 0
I can not download files at work.
As you step though the code after you run the line
Code:
Set r = Sheets("Pivot Table").Cells.Find(What:="*Grand Total*", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)

Over the curser over r to see the value or in the immediate window type
Code:
? r
If grand total is in you sheet you should see it in the string that is returned. If it is nothing then it did not find grand total.
 
Upvote 0
thanks for your input


the reason it was not working is the I had a formula search for "Grand"

I amended my macro to select Col A to search for Grand and it now works perfectly
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,097
Members
452,542
Latest member
Bricklin

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