Excel VBA: Range to String Array in 1 step Excel VBA: Range to String Array in 1 step vba vba

Excel VBA: Range to String Array in 1 step


You actually can go directly from a range to an array using the functions Split, Join and a delimiter not in the text.

Assuming you have already assigned a 1D range of values as SrcRange

Dim Array() As String: Array = Split(Join(Application.Transpose(SrcRange), "#"), "#")


How about...

Public Function RangeToStringArray(theRange As Excel.Range) As String()    ' Get values into a variant array    Dim variantValues As Variant    variantValues = theRange.Value    ' Set up a string array for them    Dim stringValues() As String    ReDim stringValues(1 To UBound(variantValues, 1), 1 To UBound(variantValues, 2))    ' Put them in there!    Dim columnCounter As Long, rowCounter As Long    For rowCounter = UBound(variantValues, 1) To 1 Step -1       For columnCounter = UBound(variantValues, 2) To 1 Step -1           stringValues(rowCounter, columnCounter) = CStr(variantValues(rowCounter, columnCounter))       Next columnCounter    Next rowCounter    ' Return the string array    RangeToStringArray = stringValuesEnd Function


Function RangeToStringArray(myRange as range) as String()    ReDim strArray(myRange.Cells.Count - 1) As String    Dim idx As Long    Dim c As Range    For Each c In myRange        strArray(idx) = c.Text        idx = idx + 1    Next c    RangeToStringArray = strArrayEnd Function