Load selection into an Array - VBA

  • Thread starter Thread starter brittonsm
  • Start date Start date
B

brittonsm

How do I take a selection of cells on the sheet and load them into an
Array in VBA?

-Steve
 
One way:

Dim vArr As Variant

vArr = Range("A1:J10").Value

which loads the values in the range into a (1 To 10, 1 to 10) variant
array.
 
brittonsm said:
How do I take a selection of cells on the sheet and load them into an
Array in VBA?

-Steve
You've received two valid answers for a Variant() array or an array
contained within a Variant variable. If that is not your situation, post
back.

Alan Beban
 
In the worst case, if your selection is a pile on non-contiguous cells, then:

Sub sel_to_array()
Dim ar() As Variant
ReDim ar(1 To Selection.Count) As Variant
i = 1
For Each r In Selection
ar(i) = r.Value
i = i + 1
Next
End Sub
 

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