How can I get a range of cells selected via user mouse input for further processing using VBA?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With