我想编写一个函数,返回 i 给定项目的所有第 j 个元素。这些项目包含单个单元格、单元格范围或两者。
虽然可以返回所有元素(test1),每个第一个元素(test2),但我无法返回每秒(或更高)的元素。
给定一个 Excel 表
B C D
2 X 1 333
3 X 2 666
4 Z 3 999
=test1((B2;C2;D2);B3:D3;(B4:C4;D4))
返回X 1 333 Y 2 666 Z 3 999
=test2((B2;C2;D2);B3:D3;(B4:C4;D4))
返回X Y Z
但是=test3((B2;C2;D2);B3:D3;(B4:C4;D4))
返回Y 2 3
,这是错误的。它应该返回1 2 3
。
VBA 函数的代码如下:
Function Test1(ParamArray argArray() As Variant)
' return all elements of all items = OK
For Each outer_arg In argArray
For Each inner_arg In outer_arg
Test1 = Test1 & " " & inner_arg
Next inner_arg
Next outer_arg
End Function
Function Test2(ParamArray argArray() As Variant)
' return only the 1st elemtent of each item = OK
For Each outer_arg In argArray
Test2 = Test2 & " " & outer_arg(1)
Next outer_arg
End Function
Function Test3(ParamArray argArray() As Variant)
' return only the 2nd elemtent of each item = FAILS
For Each outer_arg In argArray
Test3 = Test3 & " " & outer_arg(2)
Next outer_arg
End Function
如何正确处理特定元素?