I am trying to write a VBA Macro to apply conditional formatting which does the following:
If the currently selected cell is not equal to the cell to the right, change fill colour. So far I have this:
Sub Macro8()
'
' Macro8 Macro
'
' Keyboard Shortcut: Ctrl+e
'
Cells.FormatConditions.Delete
Range("G17:J17").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
Formula1:="=$K$17"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = True
End Sub
The problem lies in the range ("G17:J17")
, which is a merged cell, and the formula ("$K$17")
.
It should be Range("G$CurrentRow:J$CurrentRow")
and Formula1:="=$K$CurrentRow"
but I just don't know the syntax.
Sorry for the noobish question, but I'm a PLC programmer, not an excel guy. Thanks in advance for any help :)
Range("G17:J17")
should probably be activeCell.offset(0,1)
assuming you are selecting any cell in G column.
If you don't then you may want to tweak that - look up ActiveCell() property - it basically returns the currently activated cell(s) as a Range()
object
The formula could then be "=$K$" & activecell.row