If statement based on text color

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
At Work, I have a report that I process on a daily basis similar to this layout.


I want to create a macro that will automatically perform a calculation in Column U(as shown) for the red items.
However, the amount of rows change daily and sometimes there are also black rows and sometimes not. And sometimes I have to do this on multiple tabs(up to 3 tabs).
is an If statement possible for text color. Such as if(s4=red,s4-r4, " ").

How would I write VBA to:
- calculate S4-R4 and down for ONLY the red items,(in the format of a percent up to 3 decimal places)
- on all tabs of the workbook,
- and in the shortcut Ctrl + Shift+ J

And also within the same macro, I want to perform an "inspection" to check if all the numbers I just calculated in Column U are NOT in between -.05 and 0 percent. And if so, a pop-up box appears and reads "no violations found."

Thanks.

1633530371378.png
 

Attachments

  • 1633529299727.png
    1633529299727.png
    77.3 KB · Views: 7

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You might consider the following...

VBA Code:
Sub RedReport()
Dim ws As Worksheet
Dim r As Range, c As Range
Dim txt As String
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Sheets
    ws.Range("A3:S8" & ws.Cells(Rows.Count, "A").End(xlUp).Row).AutoFilter _
        Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterFontColor
    Set r = ws.AutoFilter.Range.Offset(0, 2).SpecialCells(xlCellTypeVisible)
    Set r = Intersect(ws.Columns("U"), r).Resize(r.Rows.Count - 1).Offset(1, 0)
    With r
        .FormulaR1C1 = "=RC[-2]-RC[-3]"
        .NumberFormat = "0.000%"
        .Font.Color = -16776961
        .Font.TintAndShade = 0
    End With
    For Each c In r
        If c.Value > -0.005 And c.Value < 0 Then
            If txt = "" Then
                txt = vbCrLf & ws.Name & " " & c.Address(0, 0)
            Else
                txt = txt & vbCrLf & ws.Name & " " & c.Address(0, 0)
            End If
        End If
    Next c
    If ws.AutoFilterMode Then
       ws.AutoFilterMode = False
    End If
Next ws
Application.ScreenUpdating = True
If txt <> "" Then
    MsgBox "violations found in:" & vbCrLf & txt
Else
    MsgBox "no violations found"
End If
End Sub

Cheers,

Tony
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,538
Members
449,038
Latest member
Guest1337

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