[英]Pasting VBA Array into Excel Range
I know this question has been asked multiple times, but I am kinda stuck adopting the suggested solutions to my problem. 我知道这个问题已被多次询问,但我有点坚持采用建议的解决方案来解决我的问题。
I have an array(0 to 4), that gets filled multiple times in a loop and should be pasted each time into a new line in excel. 我有一个数组(0到4),它在循环中被多次填充,每次都应该粘贴到excel中的新行。
Expected Output: 预期产出:
A | B | C | D | E
1 X1 | X2 | X3 | X4 | X5
My code: 我的代码:
r i = 0 To iVal
Dim infoarr(0 To 4) As Variant
infoarr(0) = ws_src_agv.Cells(ref + i + 3, 2).Value
infoarr(1) = ws_src_agv.Cells(ref + i + 4, 2).Value
infoarr(2) = ws_src_agv.Cells(ref + i + 3, 1).Value
infoarr(3) = ws_src_agv.Cells(ref + i + 3, 3).Value
infoarr(4) = ws_src_agv.Cells(ref + i + 3, 7).Value
lastR = ws_tgt_agv.Rows(Rows.Count).End(xlUp).Row
'First attempt:
ws_tgt_agv.Range(ws_tgt_agv.Cells(lastR + 1, 1), ws_tgt_agv.Cells(lastR + 1, 5)).Value = WorksheetFunction.Transpose(infoarr)
Output: 输出:
A | B | C | D | E
1 X1 | X1 | X1 | X1 | X1
2nd attempt: 第二次尝试:
ws_tgt_agv.Cells(lastR + 1, 1).Resize(UBound(infoarr, 1) + 1).Value = WorksheetFunction.Transpose(infoarr)
Ouput: 输出继电器:
A | B | C | D | E
1 X1 | | | |
2 X2
3 X3
4 X4
5 X5
if leaving the transpose argument at the end the same range gets filled with sloley X1. 如果在最后留下转置参数,则相同的范围会被sloley X1填充。
Thanks for your help! 谢谢你的帮助!
A 1-D array (both zero based and one based) is aligned like a single row with multiple columns. 一维阵列(基于零和基于一个)对齐为具有多列的单行。 You don't need to transpose in order to put te array's values into the worksheet;
您不需要转置以将te数组的值放入工作表中; you only need the correct size of target.
你只需要正确的目标大小。
with ws_tgt_agv
.Range(.Cells(lastR + 1, 1), .Cells(lastR + 1, 5)).Value = infoarr
end with
If you want to put the array's values into a single column of multiple rows then you need to transpose. 如果要将数组的值放入多行的单个列中,则需要进行转置。
with ws_tgt_agv
.Range(.Cells(lastR + 1, 1), .Cells(lastR + 6, 1)).Value = Application.Transpose(infoarr)
end with
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.