Excel VBA get range of user selected range by mouse after releasing left mouse button

Hello Experts,

I would like to extract range information from a user selected range by mouse, after making the selection, and releasing the left mouse button (or a double click after the selection has been made.

I can accomplish this with the Selectionchange event, however it's slowing down my entire worksheet. So the solution I am looking for does not rely upon the Selectionchange event.

In other words, some way of activating the following code, after the mouse selection has been made when releasing the left mouse button, or by double click.

Public Sub SelectionTest()

Dim r As Range
Dim s As String

  Select Case Selection.Areas.Count
  Case 0:
    MsgBox "Nothing selected."
  Case 1:
    MsgBox "Selected range: " & Selection.Areas(1).Address(False, False)
  Case Else
    s = ""
    For Each r In Selection.Areas
      s = s + vbNewLine + r.Address(False, False)
    Next r
    MsgBox "Selected several areas:" & s
  End Select

End Sub




Thanks,
Ray
biker9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    SelectionTest
End Sub

Open in new window

biker9Author Commented:
Thanks, but I am looking for a solution that does not rely upon the SelectionChange event.
Ray
Martin LissOlder than dirtCommented:
If you are all right with doing it off the right click then add this code to the sheet.
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
SelectionTest
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Martin LissOlder than dirtCommented:
And if that's not acceptable then this may stop the slowing of your workbook when using the SelectionChange event.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
SelectionTest
Application.EnableEvents = True
End Sub

Open in new window

aikimarkCommented:
I was going to suggest what Martin has already posted.  Use the worksheet SelectionChangeEvent, rather than the workbook version of the event.
aikimarkCommented:
You could make it more efficient by not iterating the areas.
You could make it more efficient by exiting early, if you aren't interested in the selection.
You could make it more efficient if you used debug.print, rather than msgbox.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim s As String
    Dim hdr As String
    s = Target.Address(False, False)
    Select Case Target.Areas.Count
        Case 0
            hdr = "Nothing selected."
        Case 1
            hdr = "Selected range: "
        Case Else
            hdr = "Selected several areas:" & vbCr
    End Select
    Debug.Print hdr & Replace(s, ",", vbCr)
End Sub

Open in new window

biker9Author Commented:
Thanks Martin, that did the trick, simple and effective.
Just for future reference, adding Cancel=True prior to End Sub, keeps the right click menu from popping up after running the code.
Ray
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you
including these two new ones.
Creating your own Excel Formulas and doing the impossible
A Guide to Writing Understandable and Maintainable VBA Code
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.