I have a worksheet where there are two conditional formats applied to columns U and V respectively.
I wish to apply a new conditional format to the whole relevant data range, and this include what is in columns U and V.
I recorded the following, which worked while recording.
Sub Macro13()
'
' Macro13 Macro
'
'
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
The macro is exactly as Excel produced it.
Running the macro produces an error.
"Unable to set the LineStyle property of the Border class"
occurs in
.LineStyle = xlContinuous
Why does this work when recording it but not when run? How can I change it so that it works correctly?
I am using Excel 2007 on a Windows 7 Professional computer.
I searched the site and one question was asked about this. The problem was not addressed. Instead, workarounds were offered that avoided the problem, rather than explaining why the code would not work.
UPDATE
If I insert the line Selection.FormatConditions.Delete
, like so:
Sub Macro13()
'
' Macro13 Macro
'
'
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Delete ' <-----------Added here
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
the code produces the underlining I want, but it also removes the previous conditional formatting of columns U and V, making the change useless.