Conditional formatting of a variable range

Flavien

Board Regular
Joined
Jan 8, 2023
Messages
78
Office Version
  1. 365
Platform
  1. Windows
A first code inserts new columns between the "Maxi Rating" and "MINI" columns.
As a result, the address of these columns is constantly changing.
In addition, this code will be used on different files, for which I would have a variable amount of lines (1 to 10 max).

I'd like to add conditional formatting for each active row based on the minimum and maximum range dimensions respectively ("G"; (i) and range ("H"; i).

This code is very slow and I don't know why, the code runs past the last non-empty one.

First try:
Rich (BB code):
' Find the last row with data in column A

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row

' Loop through each row from 1 to LastRow

For i = 1 To LastRow
Set rng = Range(Range("Cote_Maxi").Offset(i, 1), Range("mini").Offset(i, -1)).row(

For Each c In rng

If c.Value < Range("Cote_mini").Offset(i, 0) Then c.Interior.Color = vbYellow
If c.Value > Range("Cote_maxi").Offset(i, 0) Then c.Interior.Color = vbRed

Next c

Next i

Second try:
Rich (BB code):
' Find the last row with data in column A

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row



' Loop through each row from 1 to LastRow

For i = 1 To LastRow

Set rng = Range(Range("Cote_Maxi").Offset(i, 1), Range("mini").Offset(i, -1)).row(i)



rng.Select

Application.CutCopyMode = False

rng.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _

Formula1:="=Range(G;i)"

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

Selection.FormatConditions(1).StopIfTrue = False


Next i

I hope someone can help me.
Capture.JPG
Thank you in advance.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

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