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

shirazsafa

New Member
Joined
Nov 29, 2023
Messages
2
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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,217,394
Messages
6,136,341
Members
450,005
Latest member
BigPaws

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