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

3

3 Answers

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 Range Set myRange = Selection 

For further reading, check out the MSDN article.

1

You can loop through the Selection object to see what was selected. Here is a code snippet from Microsoft ((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 
6

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.Address End Sub 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy