Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first selected cell in a range

Tags:

excel

vba

When selecting a range in Excel manually, the first cell, or starting cell, of the selection has a different appearance. It does not have a gray layer over it like the rest of the cells in the selection.

Example 1

Example 2

I want to know the following: Is it possible to determine what the starting cell of a selection is using VBA?

If this is possible it would go into the Private Sub Worksheet_SelectionChange(ByVal Target As Range)

like image 286
SilentRevolution Avatar asked Mar 14 '26 18:03

SilentRevolution


1 Answers

Selection.Address will contain the total selected range - from top left to bottom right.

ActiveCell.Address will have the cell that was the 'start' of the selection.

You can just refer to these in the event handler:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Debug.Print Selection.Address
    Debug.Print ActiveCell.Address
End Sub
like image 52
Robin Mackenzie Avatar answered Mar 16 '26 10:03

Robin Mackenzie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!