Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get User Selected Range

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

like image 370
Talguy Avatar asked Nov 02 '10 18:11

Talguy


People also ask

How do I find the selected range in Excel?

To select an unnamed cell reference or range, type the cell reference of the cell or range of cells that you want to select, and then press ENTER. For example, type B3 to select that cell, or type B1:B3 to select a range of cells.

How do I use ActiveCell in VBA?

Type the keyword “ActiveCell”. Type a dot (.) to get the list properties and methods. Select the property or method that you want to use. Run the code to perform the activity to the active cell.

How do you specify a range in VBA?

Selecting a Single Cell Using VBARange(“A1”) tells VBA the address of the cell that we want to refer to. Select is a method of the Range object and selects the cells/range specified in the Range object. The cell references need to be enclosed in double quotes.


2 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.

like image 160
Michael Avatar answered Oct 15 '22 11:10

Michael


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 
like image 34
Miyagi Coder Avatar answered Oct 15 '22 09:10

Miyagi Coder