Determine the range of selected cells?

Gert2310
05-16-2008, 02:14 AM
Hello,

If the user selects a range in excel, how can I determine the first and the last row or column of the selected cells?

Best regards,

Gert

Cas
05-16-2008, 02:31 AM
The Application returns a Selection object that's set to the selected range, if any. To get information about its size, you use the usual range properties:
Public Sub printSelection()
Dim thisSelection As Range: Set thisSelection = Application.Selection
With thisSelection
Debug.Print .Row
Debug.Print .Rows.Count
Debug.Print .Column
Debug.Print .Columns.Count
End With
End Sub

You should include some error handling code, because Selection can return other things than a range as well, if other things than cells are currently selected.

Gert2310
05-16-2008, 05:37 AM
Hello Cas,

Thanks for the quick reply.

But what if the user selects multiple ranges? (A1 to A4 and A10 to A20)
How can I detect multiple ranges?

Best regards,

Gert

Colin Legg
05-16-2008, 05:43 AM
Hello Gert,

Look up the Range Object's Areas Property and also Areas Collection (for example code) in your Excel VBA helpfile.

Colin

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum