Thread: toggle borders
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default toggle borders

You have a giant single line "If - Then - Else If - Then" with many clauses
which is masking the logic and encouraging some code, that whilst it works I
doubt does what you intend, eg

Then .Borders(xlEdgeRight).LineStyle = xlContinuous _
And .Borders(xlEdgeTop).LineStyle = xlNone_


That's the end of the single line, this bit in its entirety
= xlContinuous And .Borders(xlEdgeTop).LineStyle = xlNone
evaluates to 0 or 1 as the value to be applied to xlEdgeTop
I assume the 'And' should be replaced with a colon :

Try the following:

Sub ToggleBorders2()
With Selection.Borders
If .Item(xlEdgeTop).LineStyle = xlNone Then
.Item(xlEdgeTop).LineStyle = xlContinuous
.Item(xlEdgeRight).LineStyle = xlNone
Else
.Item(xlEdgeTop).LineStyle = xlNone
.Item(xlEdgeRight).LineStyle = xlContinuous
End If
End With
End Sub

Regards,
Peter T


"pwilson.bowdoin" wrote in message
...
I am trying to create a macro that toggles borders around a cell. If
the cell has no borders, then the macro will place a border on the
topedge. If there is a top border, then the macro will remove the top
border and add a border on the right edge. This is what I have and it
is not working and I'm not sure why. Thanks for the help!

Sub ToggleBorder()
With Selection
If .Borders(xlEdgeTop).LineStyle = xlNone _
And .Borders(xlEdgeRight).LineStyle = xlNone _
And .Borders(xlEdgeBottom).LineStyle = xlNone _
And .Borders(xlEdgeLeft).LineStyle = xlNone _
Then .Borders(xlEdgeTop).LineStyle = xlContinuous _
Else
If .Borders(xlEdgeTop).LineStyle = xlContinuous _
And .Borders(xlEdgeRight).LineStyle = xlNone _
And .Borders(xlEdgeBottom).LineStyle = xlNone _
And .Borders(xlEdgeLeft).LineStyle = xlNone _
Then .Borders(xlEdgeRight).LineStyle = xlContinuous _
And .Borders(xlEdgeTop).LineStyle = xlNone_
End With
End Sub