Sum 2 columns on every Ws (except 3)and if they are equal to each other change their fill color

JakariKryze

New Member
Joined
Dec 14, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I need to get my code to Sum 2 columns on every Ws and if they are equal to each other change their fill color. i need to do this for 40 sheets but i have 3 sheets this doesnt need to happen for.
My current code only does the Sum for the active sheet i am on and i havent been able to get the color to work:
VBA Code:
Sub EveryDaySum()
Dim Ws As Worksheet
For Each Ws In Worksheets
 Range("E1").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[1]C:R[7]C)"
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[1]C:R[2]C)"
If Cell.Value("E1") = ("J1") Then Range("E1,J1").Select
    Range("J1").Activate
    With Selection.Interior 'Color Yellow
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
        Selection.Font.Bold = True
    End With
    End IF
    Next Ws
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
The error message and the line that triggers it is usually a must. That code shouldn't run or compile - you have an extra End If there. Where is this code, at the workbook level, sheet, or module?
Also, would you not have to activate a sheet before you can select a range if you're wanting this code to loop over sheets?
You also need a test (If / End If block) to weed out the sheets you don't want the code to act on.
 
Upvote 0
I have a version that works but you'll need to provide some answers first. It depends on where your code is.
 
Upvote 0
You could try this (after editing it for the names of the 3 sheets that you don't want it to act on) with a copy of your workbook.

VBA Code:
Sub EveryDaySum_v2()
  Dim Ws As Worksheet
  
  For Each Ws In Worksheets
    With Ws
      Select Case .Name
        Case "Sheet1", "Sheet2", "Sheet3"   '<- Names of sheets you want to skip
        Case Else
          .Range("E1").FormulaR1C1 = "=SUM(R[1]C:R[7]C)"
          .Range("J1").FormulaR1C1 = "=SUM(R[1]C:R[2]C)"
          If .Range("E1").Value = .Range("J1").Value Then
            With .Range("E1, J1")
              .Font.Bold = True
              .Interior.Color = 65535
            End With
          End If
      End Select
    End With
  Next Ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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