Thread: toggle borders
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default toggle borders

I made the guess that you wanted to continue moving the border around the
cell all the way until it turned off again. Give this code a try an see if
it does what you want...

Sub ToggleBorder()
Dim Index As Long
Dim FoundIt As Boolean
With ActiveCell
For Index = 0 To 3
If .Borders(Choose(Index + 1, xlEdgeTop, xlEdgeRight, xlEdgeBottom, _
xlEdgeLeft)).LineStyle = xlContinuous Then
FoundIt = True
Exit For
End If
Next
If FoundIt Then
ActiveCell.Borders.LineStyle = xlNone
Index = Index + 1
If Index < 4 Then
.Borders(Choose(Index + 1, xlEdgeTop, xlEdgeRight, xlEdgeBottom, _
xlEdgeLeft)).LineStyle = xlContinuous
End If
Else
.Borders(xlEdgeTop).LineStyle = xlContinuous
End If
End With
End Sub

Rick



"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