VBA - apply border to active cell

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,494
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I am using the below VBA code..
I want whole thick red color border on the active cell.
Like if cell F2 is the active cell then border on cell F2 only and remove borders if any from non active cell within the range


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("C2:F2")) Is Nothing Then
    Range("H2").Value = ActiveCell.Value
End If

If Not Intersect(Target, Range("C3:F4")) Is Nothing Then
    Range("H3").Value = ActiveCell.Value
End If

End Sub

Regards,

Humayun
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This is the best that I could do for you now :

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
    Static PreviousTarget As Range
'
    If Not PreviousTarget Is Nothing Then
        PreviousTarget.Borders.Color = xlNone                   ' 'Erase' previous border color
    End If
'
    If Not Intersect(Target, Range("C2:F2")) Is Nothing Then    ' <--- Set this to the range you want
        Target.Borders.Color = vbCyan                           ' Cyan makes the Red border when mixed with the normal border of an active cell
        Target.Borders.Weight = xlMedium                        ' Thickness of border
'
        Set PreviousTarget = Target                             ' Save the current cell address so that it can be erased the next time
'
'
'       Any additional code that you want can go here
'
'
    End If
End Sub

That will highlight the active cell border red when the cell is within the range that you specify, and it will remove the red border from that cell when another cell is clicked on.
 
Upvote 0
Hi Johnny,

Thanks for the reply... But it does not work as required
It applies borders to the entire range and does not remove when a cell is not active

I have come up with a solution (with cell color i.e. addition to my initial requirement) myself with a bit of experimenting here & there
& this does serves the purpose

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("C2:F2")) Is Nothing Then

    'This removes cell color and border from the entire range
    Range("C2:F2").Interior.Pattern = xlNone
    Range("C2:F2").Borders.LineStyle = xlNone
   
    Range("H2").Value = ActiveCell.Value
  
   'This applies cell color to the active cell
    ActiveCell.Interior.ThemeColor = xlThemeColorAccent4
    ActiveCell.Interior.TintAndShade = 0.7   
   
    'This applies border to the active cell
    ActiveCell.Borders.Color = vbBlack
    ActiveCell.Borders.Weight = xlThick   
   
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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