Get the address of active cells into array

amalghoshav

New Member
Joined
Nov 18, 2008
Messages
3
Hi,

Can anyone help with a way to get the address of active cells into an array. There can be 2 or more active cells (selected with Ctrl pressed or a range like A1:C5 etc)..

Thanks in advance

Amal

:confused:
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
amalghoshav,

Welcome to the MrExcel Board.

After you make a "Selection" of cells, then run the below macro, and it will load the array with the cell addresses.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into the Module1.

Code:
Option Explicit
Option Base 1
Sub FillArray()
    Dim MyArray As Variant
    Dim MyCount&
    Dim c As Range
    MyCount& = Selection.Count
    ReDim MyArray(MyCount&)
    MyCount& = 1
    With Selection
        For Each c In Selection
            MyArray(MyCount&) = c.Address
            'Debug.Print MyArray(MyCount&)
            MyCount = MyCount + 1
        Next
    End With
End Sub


Make a selection of cells and run the "FillArray" macro.


Have a great day,
Stan
 
Upvote 0
I'm not sure if it can be done with one line of code as you can with cell values. I played around with it for a little bit and couldn't figure it out. But it can be done with a loop

Code:
Sub SelToArray()
Dim cell As Range, SelArray As Variant, i As Integer
If Selection.Count = 1 Then Exit Sub
ReDim SelArray(1 To Selection.Count)

i = 1
For Each cell In Selection
    SelArray(i) = cell.Address
    i = i + 1
Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top