Retrive adress from selection

most

Board Regular
Joined
Feb 22, 2011
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
Can anybody explain why this happens?
With the third case I expect the result to be
$A$12
$B$13
not
$A$12
$A$13

Code:
Sub Sel()
  Debug.Print "C:" & Selection.Count
  Debug.Print Selection(1).Address
  Debug.Print Selection(2).Address
  Debug.Print "---"
End Sub

The grey areas are the cells I've been selected with Ctrl-button.
21o57uw.png
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Can anybody explain why this happens?
Yes, because your selection in the 3rd instance comprises 2 distinct areas.
Imagine if you had selected A12, A14, B13 and J1. Which cell would logically be referenced by Selection(1) or Selection(2)?

You could try
Code:
Sub Sel()
  Debug.Print "C:" & Selection.Count
  Debug.Print Selection.Areas(1).Cells(1).Address
  If Intersect(Selection, Selection.Cells(2)) Is Nothing Then
    Debug.Print Selection.Areas(2).Cells(1).Address
  Else
    Debug.Print Selection.Cells(2).Address
  End If
  Debug.Print "---"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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