Retrive adress from selection

most

Board Regular
Joined
Feb 22, 2011
Messages
107
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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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