Get User Selected Range Get User Selected Range vba vba

Get User Selected Range


Selection is its own object within VBA. It functions much like a Range object.

Selection and Range do not share all the same properties and methods, though, so for ease of use it might make sense just to create a range and set it equal to the Selection, then you can deal with it programmatically like any other range.

Dim myRange as RangeSet myRange = Selection

For further reading, check out the MSDN article.


You can loop through the Selection object to see what was selected. Here is a code snippet from Microsoft (http://msdn.microsoft.com/en-us/library/aa203726(office.11).aspx):

Sub Count_Selection()    Dim cell As Object    Dim count As Integer    count = 0    For Each cell In Selection        count = count + 1    Next cell    MsgBox count & " item(s) selected"End Sub


This depends on what you mean by "get the range of selection". If you mean getting the range address (like "A1:B1") then use the Address property of Selection object - as Michael stated Selection object is much like a Range object, so most properties and methods works on it.

Sub test()    Dim myString As String    myString = Selection.AddressEnd Sub