0

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ł

2
  • What is the problem? To determine the size of the range? Well you have the bounds (LBound and UBound)) of the array. Commented Sep 16, 2016 at 9:26
  • Do I have to set a range? Can I assing array to range after setting? Like rng = array? Commented Sep 16, 2016 at 9:42

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.