Range to Array

galore

New Member
Joined
Sep 21, 2009
Messages
9
Hi,

I can not find how to put the strings i have in a range (a1:a10) into an array of string that I can user later on with a loop.

PD: The range may have 10, 5 or 200 strings (cells) and the array (Sub) should recognize the number of items.

Thanks.

A1: John
a2: Mary
etc..

How can I do this dynamically:

Public Sub SingleDimension()

' Define a variant to hold individual strings.
Dim IndividualString As Variant

' Define the array of strings.
Dim StringArray(5) As String

' Fill each array element with information.
StringArray(0) = "This"
StringArray(1) = "Is"
StringArray(2) = "An"
StringArray(3) = "Array"
StringArray(4) = "Of"
StringArray(5) = "Strings"

' Use the For Each...Next statement to get each array
' element and place it in a string.
For Each IndividualString In StringArray

' Create a single output string with the array
' array elements.
Output = Output + IndividualString + " "
Next

' Display the result.
MsgBox Trim(Output), _
vbInformation Or vbOKOnly, _
"Array Content"
End Sub


Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi

Assuming the range is contiguous, starts in A1, down, try:

Code:
Sub RngArr()
Dim r As Range
Dim arr As Variant
 
Set r = Range("A1", Range("A1").End(xlDown))
 
' get the values of the range into an array
arr = Application.Transpose(r)
 
' display the values of the array
MsgBox Join(arr, ", ")
End Sub
 
Upvote 0
It works great. Thank very much.

Is this always like this? or is a membership needed?

Thanks again.
 
Upvote 0
It's always like this :)

Signing up is free, posting is free - you can even get humour thrown in in the cost (try reading any of Greg Truby's posts!)

We like it that way
Though sometimes you do wonder if it was possible to pay to get rid of the smartmouth comments. ;):p:biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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