调整范围以将数组值输入到工作表 vba
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24026736/
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
resizing range to enter array values into worksheet vba
提问by user3705012
If I wish to take an array and enter it into an Excel worksheet, how do I do that?
如果我想获取一个数组并将其输入到 Excel 工作表中,我该怎么做?
If I use my code below, they go into the wrong cell (G5 instead of F4) and cut off the last column and row of the array.
如果我使用下面的代码,它们会进入错误的单元格(G5 而不是 F4)并切断数组的最后一列和最后一行。
I can add 1 to each of the resize dimensions (which will give me all the values I need), but then the data still only starts in G5 rather than F4. How can I get the data to begin from F4? (I've condensed the problem to this from a much larger spreadsheet where I'm not able to just simply use the next cell). Code is as follows:
我可以为每个调整大小的维度加 1(这将为我提供我需要的所有值),但是数据仍然只从 G5 而不是 F4 开始。如何从F4开始获取数据?(我已经从一个更大的电子表格中将问题浓缩为这个问题,我不能简单地使用下一个单元格)。代码如下:
Public ArrayToPaste(4, 2) As Variant
Sub PasteTheArray()
Dim i, j As Integer
For i = 1 To 2
For j = 1 To 4
ArrayToPaste(j, i) = Cells(j, i).Value
Next j
Next i
Range("F4").Resize(UBound(ArrayToPaste, 1), UBound(ArrayToPaste, 2)) = ArrayToPaste
End Sub
回答by Gary's Student
You were very close:
你非常接近:
Public ArrayToPaste(1 To 4, 1 To 2) As Variant
Sub PasteTheArray()
Dim i, j As Integer
For i = 1 To 2
For j = 1 To 4
ArrayToPaste(j, i) = Cells(j, i).Value
Next j
Next i
Range("F4").Resize(UBound(ArrayToPaste, 1), UBound(ArrayToPaste, 2)) = ArrayToPaste
End Sub
Just make ArrayToPaste 1-basedrather than 0-based.
只需让 ArrayToPaste基于 1而不是基于 0。