Using Visual Basic to show active cell (column and row)

Status
Not open for further replies.

thisisliam

New Member
Joined
Oct 8, 2020
Messages
7
Office Version
  1. 2019
Platform
  1. MacOS
Hi all

I just implemented this method to show the active cell by highlighting the row and column.


While it works a treat, I don't like each cell in the the entire column and row being formatted. I'd like to just boarder the row and column. Using this method I'm only able to get it to boarder each and every cell within that row and column instead of just the column (left and right border) and row (top and bottom border). The selected (intersecting) cell by default is already outlined.

I would assume two conditional rules need to be defined, but I lack VBA knowledge and I'm not sure how I would modify it.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here are two different pieces of code. The first one should do as you've asked for. It outlines the selected row and column. Note, however, if your worksheet contains other borders, they will be removed. The second one highlights the row and column just like the one in your link. However, unlike the one in your link, it doesn't use conditional formatting and it doesn't require a re-calculation. Try both of them and you may find that you like the second one. :-)

Here's the one with borders...

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Cells.Borders.LineStyle = xlNone
    With ActiveCell
        With .EntireRow
            With .Borders(xlEdgeTop)
                .Color = RGB(0, 176, 80) 'green (change as desired)
                .LineStyle = xlContinuous
                .Weight = xlMedium 'or xlThin, xlThick, and xlHairline (change as desired)
            End With
            With .Borders(xlEdgeBottom)
                .Color = RGB(0, 176, 80)
                .LineStyle = xlContinuous
                .Weight = xlMedium
            End With
        End With
        With .EntireColumn
            With .Borders(xlEdgeLeft)
                .Color = RGB(0, 176, 80)
                .LineStyle = xlContinuous
                .Weight = xlMedium
            End With
            With .Borders(xlEdgeRight)
                .Color = RGB(0, 176, 80)
                .LineStyle = xlContinuous
                .Weight = xlMedium
            End With
        End With
    End With
End Sub

And here's the one with highlighting...

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Cells.Interior.ColorIndex = xlNone
    With ActiveCell
        .EntireRow.Interior.Color = RGB(219, 229, 241)
        .EntireColumn.Interior.Color = RGB(219, 229, 241)
    End With
End Sub

Hope this helps!
 
Upvote 0
If you already have existing borders around your data, then you'll need to amend the code so that after it clears all borders and before applying borders to the selected row and column, you re-draw the borders around your data.

However, I would suggest that you convert your data into a Table (Ribbon >> Insert tab >> Tables group >> Table). This way your table style will be maintained and there would be no need to re-draw your original borders every time.
 
Upvote 0
Why not just use
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Cells.Borders.LineStyle = xlNone
With ActiveCell.EntireRow.Borders
       .Color = RGB(0, 176, 80) 'green (change as desired)
       .LineStyle = xlContinuous
       .Weight = xlMedium 'or xlThin, xlThick, and xlHairline (change as desired)
End With
End Sub
 
Upvote 0
Hi Michael,

Just wanted to point out that the OP wanted borders around the column as well, and only an outline for both row and column, no inside borders...

I'd like to just boarder the row and column. Using this method I'm only able to get it to boarder each and every cell within that row and column instead of just the column (left and right border) and row (top and bottom border).

Cheers!
 
Upvote 0
@Domenic
Aha...thanks for pointing that out.
I was wondering why so much code for simply putting in borders.....?
 
Upvote 0

Domenic,​


Thanks for the code. Can you give us the code of how to do it if we need to be able to keep the existing borders? In some of our workbooks we cannot use AutoTables, we need to draw the borders manually. Therefore, we need this solution to be able not to remove the borders.

Thanks a million.
 
Upvote 0
Duplicate to: Border Around entire row of selected cell

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,808
Messages
6,181,073
Members
453,020
Latest member
mattg2448

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