简体   繁体   English

将VBA数组传递给公式

[英]Passing a VBA array to a formula

I have two rows of data looking like this: 我有两行数据看起来像这样:

数据

I am trying to return all the position numbers for each ID in an array. 我试图返回数组中每个ID的所有位置编号。 I have tried 我试过了

={IF(A2:A562=E2,B2:B562)}

But it fails whenever the ID I am searching for is not the first in column A. (I did try to sort column A with no luck). 但是每当我搜索的ID不是A列中的第一个时它就会失败。(我确实尝试对A列进行排序而没有运气)。

So I have come up with this workaround: instead I would use this formula 所以我想出了这个解决方法:相反,我会使用这个公式

={INDEX(B2:B562,positions(E2))}

where positions is a VBA function that return an array of rows that match specified ID. 其中positions是一个VBA函数,它返回与指定ID匹配的行数组。 The function positions is coded so that it return a Variant . 对函数positions进行编码,以便返回Variant But it seems that the VBA Array is not passing to the formula in excel. 但似乎VBA阵列没有传递给excel中的公式。 When I evaluate the formula, positions(E2) is equal to 0. (I have checked in VBA, and my array is correctly populated). 当我评估公式时, positions(E2)等于0.(我已检入VBA,并且我的数组已正确填充)。

So how do I make my formula correctly interpret the VBA array? 那么如何让我的公式正确解释VBA数组呢?

UPDATE: Here is my code: 更新:这是我的代码:

Function positions(idrange As Range) As Variant
Dim V As Variant
Dim l, nb As Integer
Dim id As Double

nb = 4
ReDim V(nb) As Variant

id = idrange.Value
Set cible = Sheet2.Range("B1")
For l = 1 To nb
    Set cible = Sheet2.Columns(2).Find(What:=id, After:=cible, _
                LookIn:=xlValues)

    V(l) = cible.Row - 1   
Next l
positions = Application.Transpose(V)

End Function

UPDATE 2: Here is the desired output 更新2:这是所需的输出

在此输入图像描述

Put this array formula in F2: 将此数组公式放在F2中:

=IFERROR(INDEX($B$2:$B$562,MATCH(1,($A$2:$A$562=$E2)*(COUNTIF($E$2:E2,$B$2:$B$562)=0),0)),"")

Confirm with Ctrl-Shift-Enter instead of Enter. 使用Ctrl-Shift-Enter而不是Enter确认。 If done correctly then Excel will put {} around the formula. 如果操作正确,那么Excel会在公式周围放置{}

Then copy over and down sufficient to cover all the data. 然后上下复制足以覆盖所有数据。

在此输入图像描述


EDIT #1 编辑#1

If you can sort the data then you can avoid the array formula and use this normal formula: 如果您可以对数据进行排序,那么您可以避免数组公式并使用此常规公式:

=IF(COLUMN(A:A) <= COUNTIF($A:$A,$E2),INDEX($B:$B,MATCH($E2,$A:$A,0)+COLUMN(A:A)-1),"")

在此输入图像描述

Drawing on Is it possible to fill an array with row numbers which match a certain criteria without looping? 绘图是否可以使用符合特定条件的行号填充数组而不循环? , you can do this with array and VBA as so: ,您可以使用数组和VBA执行此操作,如下所示:

  1. This line Filter(Application.Transpose(Application.Evaluate("=IF(A2:A100=OFFSET(E2," & lngCnt - 1 & ",0), (B2:B100),""x"")")), "x", False) returns a string of the matching B values 此行Filter(Application.Transpose(Application.Evaluate("=IF(A2:A100=OFFSET(E2," & lngCnt - 1 & ",0), (B2:B100),""x"")")), "x", False)返回匹配B值的字符串

"1","4","7","10" for A2 A2的“1”,“4”,“7”,“10”

  1. This line [e2].Offset(lngCnt - 1, 1).Resize(1, UBound(x) + 1) = Split(Join(x, "|"), "|") puts the string to each array 这一行[e2].Offset(lngCnt - 1, 1).Resize(1, UBound(x) + 1) = Split(Join(x, "|"), "|")将字符串放入每个数组

code

Sub GetEm()

Dim lngCnt As Long
'range of your codes from E2 down
y = [E2:E4]

For lngCnt = 1 To UBound(y)
     x = Filter(Application.Transpose(Application.Evaluate("=IF(A2:A100=OFFSET(E2," & lngCnt - 1 & ",0), (B2:B100),""x"")")), "x", False)
    [e2].Offset(lngCnt - 1, 1).Resize(1, UBound(x) + 1) = Split(Join(x, "|"), "|")
Next
End Sub

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM