Search code examples
vbaexcelexcel-2003

Get User Selected Range


How can I get a range of cells selected via user mouse input for further processing using VBA?


Solution

  • 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