Conditional formatting VBA

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
I have a sheet with a bunch of data and I want to apply conditional formatting to Column "AK"

I want to compare the value on each row from column "D" to the same row in Column "AK" If the value in "AK" is greater then "D" then conditionally format

Below is my code. It runs until it hits a cell that should be formatted and runtime error "449"

I cannot figure out the issue. It seems like perhaps I am missing a perimeter but cannot figure it out.

My error occurs at the line that start "With ws.Cells......"

Thank you in advance for the assistance.


VBA Code:
Sub ConditionalFormat()
    Dim ws As Worksheet
    Dim lastRow As Long
    Set ws = ActiveSheet

    ' Find the last row with data in column D
    lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row

    ' Loop through all rows
    For i = 2 To lastRow
        ' Check if the value in column AK is greater than the value in column G
        If ws.Cells(i, "AK").Value > ws.Cells(i, "G").Value Then
            ' If the value in column AK is greater, apply conditional formatting
            With ws.Cells(i, "AK").FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater)
                .Formula1 = "=AK" & i
                .Interior.Color = vbYellow
            End With
        End If
    Next i
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Not test but it seems your are trying to compare AK to itself?
PHP:
With ws.Cells(i, "AK").FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater)
                .Formula1 = "=AK" & i
should be?
PHP:
With ws.Cells(i, "AK").FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater)
                .Formula1 = "=G" & i
 
Upvote 0
I tried the code change but it still breaks when the first row is true as far as should be conditionally formatting.

Thank you
 
Upvote 0
I figured it out. Should read

VBA Code:
With Cells(i, "AK").FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=$G" & i)
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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