Background: I have a large Excel worksheet in which I created a "crosshair" for easy comparisons between data in the same row and column as the currently selected cell. Most "crosshair" navigation tricks I have seen use formatting, which would either delete or overwrite my existing conditional formatting. My work-around was to use a transparent line to highlight the row and column of the currently selected cell.
Problem: The code works for most selection sets, except for non-adjacent cell selections. For non-adjacent cells, it only highlights the first cell in the selection. Ex: If I select F10
then select H6
, I expect two crosshairs: one centered at F10
, and another centered at H6
. Instead, there is a single crosshair centered at F10
.
Question: Is there a way to create a selection crosshair which will work for non-adjacent cell selections?
Current Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim On_Off As Boolean
On_Off = True
If On_Off = False Then Exit Sub
Dim Sht As Worksheet
Dim Rng As Range
Set Sht = ActiveSheet
Set Rng = Selection
Dim Shp As Shape
Dim Clr As Long
Dim RWt As Double
Dim CWt As Double
Dim Trns As Double
Clr = RGB(100, 20, 180)
Trns = 0.85
RWt = Rng.Height
CWt = Rng.Width
Debug.Print Rng.Address(False, False, xlA1)
For Each Shp In Sht.Shapes
If Shp.Name = "RowLine" Or Shp.Name = "ColLine" Then
Shp.Delete
End If
Next Shp
With Sht.Shapes.AddConnector(msoConnectorStraight, 0, _
Rng.Top + Rng.Height / 2, 10000, Rng.Top + Rng.Height / 2)
.Name = "RowLine"
.Line.ForeColor.RGB = Clr
.Line.Transparency = Trns
.Line.Weight = RWt
End With
With Sht.Shapes.AddConnector(msoConnectorStraight, _
Rng.Left + Rng.Width / 2, 0, Rng.Left + Rng.Width / 2, 10000)
.Name = "ColLine"
.Line.ForeColor.RGB = Clr
.Line.Transparency = Trns
.Line.Weight = CWt
End With
End Sub