List all selected cells in a Excel

AXISHK
AXISHK used Ask the Experts™
on
How to write a macro to show all the selected cells in a current workbook ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Awarded 2010

Commented:
Hello,

what do you mean with "show all selected cells"? If you select the cells, you can already see them, so in what way do you want to "show" them?

Can you explain what you want to achieve? What is the bigger picture?

cheers, teylyn

Author

Commented:
Suppose the 3 cells in the attached file is selected, how to collect the values of all cells when I run the macro ? I want to do some calcuation for them.

Tks
selectedCells.png
Analyst Assistant
Commented:
This code will put all the values from the selected cells in array.

Not sure it's what you want but it might give you a start.
Sub GetSelectedValues()
Dim rng As Range
Dim cl As Range
Dim arrVals()
Dim I As Long

    Set rng = Selection
    
    For Each cl In rng
    
        ReDim Preserve arrVals(I)
        
        arrVals(I) = cl.Value
        
        I = I + 1
        
    Next cl
End Sub

Open in new window

Most Valuable Expert 2011
Awarded 2010

Commented:
You could use a for/each loop along the lines of

Dim cel As Range

For Each cel In Selection
    ' your calculation, for example assign a value to each cell
    cel = cel.value * 1
Next

Open in new window

Author

Commented:
Tks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial