multi cells selected

  • Thread starter Thread starter greg
  • Start date Start date
G

greg

Can I tell if multi cells are selected? And can I get each cell?
So if i
click A1
Ctrl+click D1
Ctrl+click G1

Can I get each of these cells?
thanks
 
Can I tell if multi cells are selected? And can I get each cell?
So if i
click A1
Ctrl+click D1
Ctrl+click G1

Can I get each of these cells?
thanks

Hello Greg,

The Range object has an Areas collection property. If you select a
single cell or a contiguous range of cells then Areas.Count = 1. Here
is a code snippet to display what Ranges you have selected in message
box.

Sub ShowRanges()

Dim Msg As String
Dim Rng As Range

For Each Rng In Selection.Areas
Msg = Msg & Rng.Address & vbCrLf
Next Rng

MsgBox "You selected the following ranges..." & vbCrLf & Msg

End Sub

Sincerely,
Leith Ross
 
thanks!
that worked

Leith Ross said:
Hello Greg,

The Range object has an Areas collection property. If you select a
single cell or a contiguous range of cells then Areas.Count = 1. Here
is a code snippet to display what Ranges you have selected in message
box.

Sub ShowRanges()

Dim Msg As String
Dim Rng As Range

For Each Rng In Selection.Areas
Msg = Msg & Rng.Address & vbCrLf
Next Rng

MsgBox "You selected the following ranges..." & vbCrLf & Msg

End Sub

Sincerely,
Leith Ross
 
But you still could get the address of each cell in the selection:

Dim myCell as range
for each mycell in selection.cells
msgbox mycell.address(0,0)
next mycell

and you can check with this kind of code.

if selection.cells.count > 1 then
'multiple cells selected
end if

if selection.areas.count > 1 then
'multiple areas selected
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top