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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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