What is the best way to convert a range of cells to a string? I have a function that only takes a string as input so I need to convert the range to a string, while retaining
To make a comma separated list of cell values in a range:
Function RangeToString(ByVal myRange as Range) as String
RangeToString = ""
If Not myRange Is Nothing Then
Dim myCell as Range
For Each myCell in myRange
RangeToString = RangeToString & "," & myCell.Value
Next myCell
'Remove extra comma
RangeToString = Right(RangeToString, Len(RangeToString) - 1)
End If
End Function
You could add extra functionality like inserting a semicolon instead of a comma if the row number increases.
To use this function:
Sub AnySubNameHere()
Dim rng As Range
Set rng = ActiveSheet.Range("A3:A10")
Dim myString as String
myString = RangeToString(rng)
End Sub