vba 将数组复制到单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7894884/
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
Copying an array to cells
提问by user1012091
I currently have an array full of data that I would like to transfer to cell G2 all the way until the entire array is exhausted. My current spreadsheet has data in G1 but no data under that.
我目前有一个充满数据的数组,我想一直传输到单元格 G2,直到整个数组用完为止。我当前的电子表格在 G1 中有数据,但没有数据。
I have the following code but it doesn't exactly work because I get the error:
我有以下代码,但它并不完全有效,因为我收到错误:
Application-defined or object-defined error.
应用程序定义或对象定义的错误。
Help fixing this problem would be great. I would appreciate if you could tell me what is wrong with my code and how to fix it rather than providing me with an alternative formulation.
帮助解决这个问题会很棒。如果您能告诉我我的代码有什么问题以及如何修复它而不是为我提供替代公式,我将不胜感激。
For i = 1 to nFlights
With Worksheets("Q2").Range("G1")
.End(xlDown).Offset(1, 0) = Origin(i)
End With
Next
回答by Tim Williams
I know you didn't want to be shown code, but here's a different approach from your loop:
我知道您不想显示代码,但这是与您的循环不同的方法:
Dim arr
arr = Array("one","two","three","four")
ActiveSheet.Range("G2").Resize((UBound(arr) - LBound(arr)) + 1, 1).Value = _
Application.Transpose(arr)
回答by Michael Kingsmill
The offset property just references the cell (in your case), one below the cell that is the base of the call ("G1"). In order to copy the whole array orgin into the column G on the sheet try this:
offset 属性仅引用单元格(在您的情况下),位于作为调用基础的单元格下方的单元格(“G1”)。为了将整个数组原点复制到工作表上的 G 列中,请尝试以下操作:
Dim i As Integer
i = 2
For Each val As String In Origin
With Worksheets("Q2")
.Cells(i, 7).Value = Origin(i)
i = i + 1
End With
Next