the problem is i have data with a filter applied so when the color is applied it applying to the entire column i want to apply color only to those rows which is filterred
this is ok but here in that particular cells have a formula ,so when the color is applied formula is missing is there any that formula should not be disturbed
Sub ApplyRedColorToFilteredRows()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim columnNumber As Integer
' Set the worksheet you want to work with
Set ws = ThisWorkbook.Sheets("Sheet1")
' Set the column number you want to apply the color to
columnNumber = 1 ' Example: Column A
' Get the range of the column excluding the header
Set rng = ws.Range(ws.Cells(2, columnNumber), ws.Cells(ws.Rows.Count, columnNumber).End(xlUp))
' Apply red color to the text in each visible cell in the range
For Each cell In rng
If cell.EntireRow.Hidden = False Then
If cell.Value <> "" Then
cell.Font.Color = RGB(255, 0, 0)
End If
End If
Next cell
End Sub
Please try this it works for me i hope it also works for you
Output:
As you see i applied filter for 2024 it does not colour that row,it colours remaining data
let me try on this thank you
in that row i have a formula been applied when color is applied formula is getting effected formula is missing there
how can apply colurs to diffent columns with different in the same macro
Sub ApplyRedColorToFilteredRows()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim columnNumber As Integer
' Set the worksheet you want to work with
Set ws = ThisWorkbook.Sheets("Sheet1")
' Set the column number you want to apply the color to
columnNumber = 1 ' Example: Column A
' Get the range of the column excluding the header
Set rng = ws.Range(ws.Cells(2, columnNumber), ws.Cells(ws.Rows.Count, columnNumber).End(xlUp))
' Clear any existing conditional formats in the range
rng.FormatConditions.Delete
' Apply conditional formatting to each visible cell in the range
For Each cell In rng
If cell.EntireRow.Hidden = False Then
With cell.FormatConditions.Add(Type:=xlExpression, Formula1:="=ROW()=" & cell.Row)
.Font.Color = RGB(255, 0, 0)
End With
End If
Next cell
End Sub
Macros Code more than one column
Sub ApplyColorsToFilteredRows()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim columnNumbers As Variant
Dim colors As Variant
Dim i As Integer
' Set the worksheet you want to work with
Set ws = ThisWorkbook.Sheets("Sheet1")
' Define the columns you want to format and their corresponding colors
columnNumbers = Array(1, 2, 3) ' Example: Columns A, B, C
colors = Array(RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255)) ' Red, Green, Blue
' Loop through each specified column
For i = LBound(columnNumbers) To UBound(columnNumbers)
' Get the range of the column excluding the header
Set rng = ws.Range(ws.Cells(2, columnNumbers(i)), ws.Cells(ws.Rows.Count, columnNumbers(i)).End(xlUp))
' Clear any existing conditional formats in the range
rng.FormatConditions.Delete
' Apply conditional formatting to each visible cell in the range
For Each cell In rng
If cell.EntireRow.Hidden = False Then
With cell.FormatConditions.Add(Type:=xlExpression, Formula1:="=ROW()=" & cell.Row)
.Font.Color = colors(i)
End With
End If
Next cell
Next i
End Sub
Output:
I hope this will work for you
If above one works for you please mark that as solution to close the loop.