title: Vba apply conditional formatting top/bottom border only?
body: <p>I am using the following vba code to apply conditional formatting.</p>
  
  <pre><code>Sub ResetConditions()
      With Worksheets(1).Range("A9:P1048576")
          .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))"
          With .FormatConditions(.FormatConditions.Count)
              .SetFirstPriority
  
              With .Borders
              .LineStyle = xlContinuous
              .Weight = xlThin
              .Color = vbRed
              End With
  
          End With
      End With
  End Sub
  </code></pre>
  
  <p>The border is showing as this:</p>
  
  <p><a href="https://i.stack.imgur.com/NiPCq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NiPCq.png" alt="enter image description here"></a></p>
  
  <p>But i want it to look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/31Zgc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/31Zgc.png" alt="enter image description here"></a></p>
  
  <p>I am trying to set only top/bottom borders like so:</p>
  
  <pre><code>Sub ResetConditions()
          With Worksheets(1).Range("A9:P1048576")
              .FormatConditions.Add Type:=xlExpression, Formula1:= _
                "=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))"
              With .FormatConditions(.FormatConditions.Count)
                  .SetFirstPriority
  
                  With .Borders(xlEdgeTop)
                  .LineStyle = xlContinuous
                  .Weight = xlThin
                  .Color = vbRed
                  End With
  
              End With
          End With
      End Sub
  </code></pre>
  
  <p>But i keep getting an error unable to set linestyle property of border class.</p>
  
  <p>Please can someone show me where i am going wrong?</p>