excel - conditional formatting - only for the last 50 rows of the tabl

shirazsafa

New Member
Joined
Nov 29, 2023
Messages
5
Office Version
  1. 2003 or older
Platform
  1. Windows
hi
Please help me if possible
My formula is not working properly -
Because the range for formatting is variable
When the start of the range is from row 100 onwards.
Because in the formula written in conditional formatting, the address is for the previous rows
The format no longer works

VBA Code:
Sub formatting()
Dim rng As Range
Dim lr As Long

 lr = Sheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row - 40

  Range("W" & lr).Resize(50, 1).Select
'Suppose this is limited from row 200 to 250

  Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($G2<>"""";$W2<>"""";$W2<4)"
'Because cell $G2 is mentioned in the formula. And now this cell is not in the area. For this reason, the above formula fails

    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
End Sub

thx . safa
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
If you make the row a variable, it seems like you could handle it like this:

VBA Code:
Sub formatting()
Dim rng As Range
Dim lr As Long, format_rng As Range, start_row As Long

 lr = Sheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row - 49 ' why was it 40 instead of 49?

  Set format_rng = Range("W" & lr).Resize(50, 1)
  start_row = format_rng(1, 1).Row
'Suppose this is limited from row 200 to 250

  'Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($G2<>"""";$W2<>"""";$W2<4)"
    format_rng.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($G" & start_row & "<>"""",$W" & start_row & "<>"""",$W" & start_row & "<4)"
'Because cell $G2 is mentioned in the formula. And now this cell is not in the area. For this reason, the above formula fails

    format_rng.FormatConditions(format_rng.FormatConditions.Count).SetFirstPriority
    With format_rng.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With format_rng.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
End Sub
 
Last edited:
Upvote 0
If you make the row a variable, it seems like you could handle it like this:

VBA Code:
Sub formatting()
Dim rng As Range
Dim lr As Long, format_rng As Range, start_row As Long

 lr = Sheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row - 49 ' why was it 40 instead of 49?

  Set format_rng = Range("W" & lr).Resize(50, 1)
  start_row = format_rng(1, 1).Row
'Suppose this is limited from row 200 to 250

  'Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($G2<>"""";$W2<>"""";$W2<4)"
    format_rng.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($G" & start_row & "<>"""",$W" & start_row & "<>"""",$W" & start_row & "<4)"
'Because cell $G2 is mentioned in the formula. And now this cell is not in the area. For this reason, the above formula fails

    format_rng.FormatConditions(format_rng.FormatConditions.Count).SetFirstPriority
    With format_rng.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With format_rng.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
End Sub
I really don't know how to thank you. Thank you, a thousand times,
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

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