将字符串数组复制到一系列单元格中(在 MS Excel 中使用 VBA)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16983331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:48:01  来源:igfitidea点击:

Copying a String Array into a Range of cells (in MS Excel using VBA)

excelexcel-vbavba

提问by Kent Pawar

I can use a Forconstruct 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?

我可以使用一个For构造来遍历字符串数组元素并将它们的内容复制到范围的各个单元格中;但是有没有更简单的方法可以将字符串数组项直接复制到 Range 中?

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

问题Range to string array解决了与我正在尝试做的完全相反的问题。

回答by

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, thisfor more examples and tutorial

此外,是更多示例和教程

EDIT:
this documentationexplains why the WorksheetFunction.Transposewas used

编辑:
文档解释了为什么WorksheetFunction.Transpose使用

回答by gssi

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}

你真的需要一个单元格中的数组吗?请记住,您可以定义一个等于数组值的变量: var1 = {v11, v12, v13;v21, v22, v23}

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

然后将“=INDEX(var1, 2, 2)”放入任何单元格中以获得值v22。

回答by Patrick Lepelletier

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.

数组设置在一行中,我不使用转置。