The usual issue is to pass range to array, but this time I need to do the opposite. I want to pass various size string array to a range. BR Michał
-
What is the problem? To determine the size of the range? Well you have the bounds (LBound and UBound)) of the array.Axel Richter– Axel Richter2016-09-16 09:26:51 +00:00Commented Sep 16, 2016 at 9:26
-
Do I have to set a range? Can I assing array to range after setting? Like rng = array?Eswemenasja– Eswemenasja2016-09-16 09:42:06 +00:00Commented Sep 16, 2016 at 9:42
Add a comment
|
1 Answer
To set the value of a range from an array, we must first determine the size of that range dependent of the arrays size. For this we can use the bounds (LBound and UBound) of the array.
Then, if the array fits into the range, we can simply do range.value = arr.
Example:
Sub test()
Cells.ClearContents
' one dimensional array; base 0
aSArr = Array("This", "is", "an", "array", "of", "strings")
Set r1 = Range("C3").Resize(1, 1 + UBound(aSArr))
r1.Value = aSArr
Set r2 = Range("C5").Resize(1 + UBound(aSArr), 1)
r2.Value = Application.Transpose(aSArr)
' two dimensional array; base 1
aSArr = [{"This", "is", "an"; "array", "of", "strings"}]
Set r3 = Range("E6").Resize(UBound(aSArr, 1), UBound(aSArr, 2))
r3.Value = aSArr
End Sub