Selection.Borders Help

jkd_1

New Member
Joined
Jan 29, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi - to preface I am very new to VBA so hopefully this makes sense. I am trying to write a code that allows for a keystroke to toggle a cell border on and off. For example, by pressing ctrl + alt + K would add a top border. Repeating that keystroke would remove it. I am able to write the application.Onkey portion. I am struggling with the Selection.borders portion of the code. Happy to provide more detail if need be, hopefully this makes sense!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Welcome to the Board!

Try something like this code:
VBA Code:
Sub ToggleTopBorder()
    
    Dim cell As Range
    
    For Each cell In Selection
        If cell.Borders(xlEdgeTop).LineStyle = xlNone Then
            cell.Borders(xlEdgeTop).LineStyle = xlContinuous
        Else
            cell.Borders(xlEdgeTop).LineStyle = xlNone
        End If
    Next cell
    
End Sub
and then you can assign a keyboard shortcut to this code to run it (you cannot do CTL+ALT+K, but you can do CTL+SHIFT+K).

Does that work for you?
 
Upvote 0
Rich (BB code):
Sub ToggleTopBorder()
  
    Dim cell As Range
  
    For Each cell In Selection
        If cell.Borders(xlEdgeTop).LineStyle = xlNone Then
            cell.Borders(xlEdgeTop).LineStyle = xlContinuous
        Else
            cell.Borders(xlEdgeTop).LineStyle = xlNone
        End If
    Next cell
  
End Sub
Just noting the red highlighted lines of code can be replace by this single line of code...
VBA Code:
cell.Borders(xlEdgeTop).LineStyle = xlContinuous + xlNone - cell.Borders(xlEdgeTop).LineStyle
Note: The above assumes the top borders will only ever be continuous or none to begin with.
 
Upvote 0
Welcome to the Board!

Try something like this code:
VBA Code:
Sub ToggleTopBorder()
   
    Dim cell As Range
   
    For Each cell In Selection
        If cell.Borders(xlEdgeTop).LineStyle = xlNone Then
            cell.Borders(xlEdgeTop).LineStyle = xlContinuous
        Else
            cell.Borders(xlEdgeTop).LineStyle = xlNone
        End If
    Next cell
   
End Sub
and then you can assign a keyboard shortcut to this code to run it (you cannot do CTL+ALT+K, but you can do CTL+SHIFT+K).

Does that work for you?
Apologies for the delay here, this project got put on the backburner a bit as other projects took priority. This works almost perfectly, however, I have one issue. I have slightly modified to code have the Dim be "selection" instead of a "cell" (so when a selection of multiple rows is highlighted it doesn't apply top borders to every row, just the top of the selection) and that seemed to work just fine, but when I try to use this, it only works for every other row in the sheet. It seems as though it is recognizing every other row as the bottom of the previous row. Ideally, I would like to be able to add a top border to any cell or range that is selected. Hopefully, this makes sense. Happy to explain further if helpful.
 
Upvote 0
Try this version:
VBA Code:
Sub ToggleTopBorder()
    
    With Selection
        If .Borders(xlEdgeTop).LineStyle = xlNone Then
            .Borders(xlEdgeTop).LineStyle = xlContinuous
        Else
            .Borders(xlEdgeTop).LineStyle = xlNone
        End If
    End With
    
End Sub
 
Upvote 0
Solution
Try this version:
VBA Code:
Sub ToggleTopBorder()
  
    With Selection
        If .Borders(xlEdgeTop).LineStyle = xlNone Then
            .Borders(xlEdgeTop).LineStyle = xlContinuous
        Else
            .Borders(xlEdgeTop).LineStyle = xlNone
        End If
    End With
  
End Sub
That's perfect. Exactly what I was looking for thanks
 
Upvote 0
You are welcome.
Glad it worked for you!
:)
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top