0

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.

Community
  • 1
  • 1
  • Try removing the three lines which start with `.` as these are the default settings? – CJC Jan 06 '17 at 20:21
  • 1
    I can't get it to throw an error. It works for me even with existing formatting. – Scott Craner Jan 06 '17 at 20:21
  • I can't get an error to occur when running this code; however, I do have a thought why it might not work... when pressing F8 (in VBA), each part of the code will be executed individually. Can you see if something is happening just before the error which might reset "selection" orientation? What exactly is the error that shows up? – Cyril Jan 06 '17 at 20:28
  • 1
    Do you have any worksheet protection on the worksheet in question? For VBA to change formatting, the worksheet must be unlocked. – deasa Jan 06 '17 at 20:28
  • @CJC, if I remove those instructions, then nothing happens. I could delete the last two, but not the .LineStyle one. That's the one I really need. It does an underlining of the appropriate cells – Bruno Repetto Jan 06 '17 at 20:57
  • @Cyril, Nothing happens to with the selection. The exact error is 'Run-time error '1004': Unable to set the LineStyle property of the Border class. – Bruno Repetto Jan 06 '17 at 20:59
  • @BGeorge - the sheet is not protected. – Bruno Repetto Jan 06 '17 at 20:59
  • Ok, try removing the `With` `End With`, also remove the last two lines with the dot. Then delete the empty space so that LineStyle instruction is part of the previous line. (NB I'm without Excel at the moment so can't test) – CJC Jan 06 '17 at 21:07
  • Try looking at http://stackoverflow.com/questions/39033906/unable-to-set-the-linestyle-property-of-the-border-class as a reference. It might be an issue with the border class. If that is the case, then it would mean that the recorder is taking explicit instructions (logic flow), when the input for those instructions require separate means to execute. – Cyril Jan 06 '17 at 21:12
  • Just saw your update. Try changing SetFirstPriority to SetSecondPriority - that's a guess but you already have one rule active – CJC Jan 06 '17 at 21:14
  • FWIW - Your code works fine for me, leaving existing conditional formatting untouched. – YowE3K Jan 06 '17 at 21:54
  • @CJC, I tried that when you made your first suggestion. Same error. (removing some lines and getting rid of the With/End With. Also, there is no such thing as "SetSecondPriority". Only "SetLastPriority", and that doesn't work either. – Bruno Repetto Jan 06 '17 at 21:58
  • @YowE3K, are you using Excel 2007? – Bruno Repetto Jan 06 '17 at 22:02
  • @ScottCraner, are you using the same version I'm using, Excel 2007? – Bruno Repetto Jan 06 '17 at 22:02
  • @Cyril, it does not appear that the recorder is taking explicit instructions, as you indicate. I've tried several permutations of including and not including the .Borders(...) qualifier to no avail. – Bruno Repetto Jan 06 '17 at 22:05
  • My version is Excel 2010 – YowE3K Jan 06 '17 at 22:14
  • When you tried `SetLastPriority`, did you also change `Selection.FormatConditions(1)` to `Selection.FormatConditions(Selection.FormatConditions.Count)` ? – YowE3K Jan 06 '17 at 22:16
  • @YowE3K, I know that the macro recorders of versions 2007 and 2010 respond very differently from each other. That right there might be the problem. Unfortunately, my organization will not support an upgrade at this time. My answer to your question is yes, I did make the change. – Bruno Repetto Jan 06 '17 at 22:29
  • The only other thing that I'm finding anywhere is that there may be something corrupt with the source file; sorry I can't be of more help on the topic! Good luck finding an explanation, Bruno! I'll check back in to see if anything turns up. – Cyril Jan 06 '17 at 23:26
  • Thanks to all that tried to help me. I ended up finding a workaround that doesn't mess with conditional formatting at all, and the code turned out to be a lot simpler than trying it with conditional formatting. Unfortunately, as was my expressed hope, (see my OP), I never did find out why my attempts didn't work. – Bruno Repetto Jan 09 '17 at 19:16
  • Hey @BrunoRepetto, a few days late to this discussion but see my answer for a possible solution. Seems to work in my tests, explained around what the recorder was doing and a better way to do the same! Hope it helps – Wolfie Jan 13 '17 at 10:37

1 Answers1

0

See commented code below for explanation. Also see this link on how to avoid using Select - something the macro recorder is notorious for!

Sub AddFormatting()

    ' Create a range object so Select isn't needed, use the same xlToRight and xlDown methods though

    Dim myRange As Range

    With ActiveSheet
        Set myRange = .Range(.Range("A1"), .Range("A1").End(xlToRight).End(xlDown))
    End With

    ' Note you may not want to use the above method, as it formats the whole document if there is nothing in row 1!!
    ' You could remove the above With block and consider using instead:
    ' Set myRange = ActivesSheet.UsedRange

    ' Add a new Format Condition
    myRange.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"

    ' Make it the first Format Condition (Macro recorder's default)
    ' Note you may not want this priority order in your conditional formats!
    ' For now though, it makes referencing the format simpler as it's now FormatConditions(1)

    myRange.FormatConditions(myRange.FormatConditions.Count).SetFirstPriority

    With myRange.FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .TintAndShade = 0
        .Weight = xlThin
    End With

    ' Again, this is excel's default but may not be what you want, look up StopIfTrue
    myRange.FormatConditions(1).StopIfTrue = False

End Sub

A final note:

You may actually want to delete all previous conditional formatting in your sheet at the start of this sub, and recreate them in the same way in VBA. This is because interacting with cells in Excel often splits up and complicates the conditional formats - creating a document which slowly grinds to a halt! It would also ensure you don't double-add the conditional format which is actually created above.

Hope this helps.

Community
  • 1
  • 1
Wolfie
  • 27,562
  • 7
  • 28
  • 55