Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A1") = False Then Exit Sub
On Error Resume Next
Dim myRange As Range
Dim myTopLeftCell As Range
Dim myBottomRightCell As Range
Set myTopLeftCell = Range("B3")
Set myBottomRightCell = Range("H17")
If Target.Row >= myTopLeftCell.Row And _
Target.Offset(Selection.Rows.Count - 1).Row <= myBottomRightCell.Row And _
Target.Column >= myTopLeftCell.Column And _
Target.Offset(, Selection.Columns.Count - 1).Column <= myBottomRightCell.Column Then
Set myRange = Selection
If ActiveSheet.Shapes("hSelection") Is Nothing Then
ActiveSheet.Shapes.AddShape(msoShapeRectangle, myTopLeftCell.Left, Selection.Top, _
myBottomRightCell.Offset(, 1).Left - _
myTopLeftCell.Left, Selection.Height).Select
With Selection
With .ShapeRange
.Fill.Visible = msoFalse
.Line.ForeColor.SchemeColor = 12
.Line.Weight = 2.25
.ZOrder msoSendToBack
.Shadow.Visible = msoFalse
End With
.Name = "hSelection"
.PrintObject = False
End With
Else
With ActiveSheet.Shapes("hSelection")
.Left = myTopLeftCell.Left
.Top = Selection.Top
.Width = myBottomRightCell.Offset(, 1).Left - myTopLeftCell.Left
.Height = Selection.Height
.ShapeRange.Shadow.Visible = msoFalse
End With
End If
myRange.Select
End If
Set myTopLeftCell = Nothing
Set myBottomRightCell = Nothing
Set myRange = Nothing
End Sub
|