Apply Conditonal Formatting based on a relative cell address in VBA

pizzaboy

Board Regular
Joined
Mar 23, 2015
Messages
56
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to add a couple of FormatConditions to a range based on Formulas (xlExpression?).


The range spans a couple of table columns so I need to build the formula based on a relative cell address so the formatting is referencing the cell's own value.


I have this working fine via Excel's Conditional Formatting, but I'm struggling with the VBA code.


Building the range was fine:
Code:
                'get the range
                rangeAdd = Range(tb & "[" & currentDay.ColumnName(formattingItem) & "]")
                
                
                'remove existing formatting
                rangeAdd.FormatConditions.Delete
                
                
                'add to the new range
                If Not rangeFormatting Is Nothing Then
                    Set rangeFormatting = Union(rangeFormatting, rangeAdd)
                Else
                    Set rangeFormatting = rangeAdd
                End If


The conditional formatting in Excel, looks like this:
Code:
=(AQ6="TOP")


How do I convert my first table cell reference so it reads "AQ6", and how do I build the formatting condition in VBA?


Code:
    Dim condition_Top As FormatCondition
    
    With rng
        Set condition_Top = .FormatConditions.Add(xlExpression, "=" & sAddress & "=""TOP""")
    End With
End Sub


Public Sub SetFormattingCondition(condition As FormatCondition, cellColor)


    With condition
        .Interior.Color = cellColor
    End With


End Sub




Any help would be greatly appreciated.


Thanks.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Got everything working!

Code:
Public Sub SetFormattingCondition(rng As Range, expression As String, cellColor)
    Dim condition As FormatCondition
    
    With rng
        Set condition = .FormatConditions.Add(xlExpression, , "=" & expression)
    End With


    With condition
        .StopIfTrue = True
        .Interior.Color = cellColor
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,085
Members
449,206
Latest member
ralemanygarcia

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