Place values of user chosen cells into array

Reset

Board Regular
Joined
Apr 16, 2010
Messages
227
I need to place a group of values form cells chosen by the user in to an array. These could be discontinuous cells (mutitple choices by holding down the control key and clicking on cells). I have not got far...

a = Application.WorksheetFunction.CountA(Selection)
ReDim x(a) as Long

So now I have the count of number of highlighted cells and an array fitted to that size. How do I place the values of selected cells into the array?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
See if this makes sense

Code:
Sub SelectionToArray()
    Dim arr()
    Dim Cell As Range
    'Redim array to number of cells selected
    ReDim arr(Selection.Cells.Count - 1)
    'Create a tracker to tell where you are in the array
    ArrIdx = LBound(arr)
    'Cycle through the cells and add their values to the array
    For Each Cell In Selection
        arr(ArrIdx) = Cell.Value
        ArrIdx = ArrIdx + 1
    Next
    'Output the array to a messagebox
    Msg = ""
    For ArrIdx = LBound(arr) To UBound(arr)
        Msg = Msg & arr(ArrIdx) & vbLf
    Next
    MsgBox Msg, vbOKOnly, "Contents of Array"
End Sub
 
Upvote 0
Here's another version

Code:
[COLOR=blue]'In a new Module
[/COLOR]Public Type SelectionCellInfo
    cellWSheet As String
    cellAddress As String
    cellValue As Variant
    cellText As String
End Type
Sub SelectionToArrayVerbose()
    Dim arr() As SelectionCellInfo
    Dim Cell As Range
    'Redim array to number of cells selected
    ReDim arr(Selection.Cells.Count - 1)
    'Create a tracker to tell where you are in the array
    ArrIdx = LBound(arr)
    'Cycle through the cells and add their values to the array
    For Each Cell In Selection
        arr(ArrIdx).cellWSheet = Cell.Worksheet.Name
        arr(ArrIdx).cellAddress = Cell.AddressLocal
        arr(ArrIdx).cellValue = Cell.value
        arr(ArrIdx).cellText = Cell.text
        ArrIdx = ArrIdx + 1
    Next
    'Output the array to a messagebox
    Msg = ""
    For ArrIdx = LBound(arr) To UBound(arr)
        Msg = Msg & arr(ArrIdx).cellWSheet & "!" & _
        arr(ArrIdx).cellAddress & _
        " V: " & arr(ArrIdx).cellValue & _
        " T: " & arr(ArrIdx).cellText & vbLf
    Next
    MsgBox Msg, vbOKOnly, "Contents of Array"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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