2

I can use a For construct to loop through the string array elements and copy their contents into the individual cells of the range; but is there a simpler way to directly copy the string array items into the Range?

The question Range to string array solves the exact opposite of what I am trying to do.


Updates:

Here is what I got till now:

' Create a header: Write a list of Fields from Array into Range
Dim strTest As String
Dim strArray() As String
Dim firstCellOftheTargetRange, lastCellOftheTargetRange  as String

strTest = "PERIOD,YEAR,SCENARIO,ANALYTICS,ENTITY,ACC_NUMBER,AMOUNT"
strArray = Split(strTest, ",")

firstCell = "A1" ' First Cell Of the Target Range
lastCell = convertNumberToColumnName(UBound(strArray) + 1) & "1" ' last Cell Of the Target Range 

Worksheets("SheetName").Range(firstCell & ":" & lastCell) = strArray

This code doesn't use WorksheetFunction.Transpose as it outputs row-wise. If you require the output to be column-wise use the function as shown by @mehow. Thanks

3 Answers 3

8

Like this

Sub StringArrayToRange()

    Dim strArr(3) As String
    strArr(0) = "one"
    strArr(1) = "two"
    strArr(2) = "three"

    Range("A1:A" & UBound(strArr) + 1) = WorksheetFunction.Transpose(strArr)

End Sub

also, this for more examples and tutorial

EDIT:
this documentation explains why the WorksheetFunction.Transpose was used

Sign up to request clarification or add additional context in comments.

6 Comments

Hi mehow, Thanks for the link and code. I tweaked around a bit and don't quite get why WorksheetFunction.Transpose is required? My sample seems to work fine without the function
Hi @Santosh - how does WorksheetFunction.Transpose help in the above code. The sample I posted seems to work fine without it.. Does it help with compatibility across older versions?
@KentPawar please refer to the link in my answer, but short story shorter (hehe) one dimensional array is in a horizontal view so you are converting it into a vertical to be technically correct
Thanks @mehow.. Looking at it now.
Argh.. I just realized your code requires WorksheetFunction.Transpose as it is output-ing into a column while my code outputs row-wise hehe.. I need a break :p. Thanks, marked it as answered
|
0

Do you really need the array in a cell? Remember, you can define a variable equal to an array value: var1 = {v11, v12, v13;v21, v22, v23}

then put "=INDEX( var1, 2, 2)" in any to cell to get value v22.

1 Comment

Hi @gssi, thanks. I needed the array items into a individual cells separately, but I want to do it using VBA and the target sheet is created dynamically so that won't work.
0

i had an other way doing this:

Dim Arr()
With ThisWorkbook.Sheets("MassHeals")
  Arr = Array("1", "2", "3", "4")
  .Cells(1, 1).Resize(1, 4).value2 = Arr 
End With

The array is set in one line, and i do not use transpose.

1 Comment

Transpose is needed only when you write a one dimension array in a column, not when you write it in a row as you do here

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.