将matrix(?)传递给Excel中的VBA

Excel中的一个常见解决scheme是带有多个条件的SUMIF(),如下所示(此公式计算列A的值为C1,列B的值为D1的所有情况):

=SUMPRODUCT((A1:A9=C1)*(B1:B9=D1)) 

我现在需要一个函数来连接来自满足多个条件的行的string。 在http://www.ms-office-forum.net/forum/showthread.php?t=273352有一个很好的解决scheme

但是,我想检查多个条件 – 因此使用上面的SUBPRODUCT()技巧。 我的问题是:如何在函数中定义参数来获取数组? 这是我到目前为止:

 ' ********************************************************************** ' Modul: Modul1 Typ: Allgemeines Modul ' ********************************************************************** Option Explicit Public Function CONCATIF(Kriterium, Inhalte) Dim mydic As Object Dim L As Long Set mydic = CreateObject("Scripting.Dictionary") For L = 1 To UBound(Kriterium) If Kriterium(L, 1) = 1 Then If Inhalte(L, 1) <> "" Then mydic(L) = Inhalte(L, 1) End If End If Next CONCATIF = Join(mydic.items, vbCrLf) End Function 

这工作正常,如果我select一列参数1.但是,只要我包括一个产品公式(如上面),只有值为1的Variant / Double传递给Kriterium

 =CONCATIF(A1:A9; E1:E9) Works fine (if column A is 0/1 coded) =CONCATIF((A1:A9=C1)*(B1:B9=D1); E1:E9) Does not work at all 

有什么build议么? 谢谢!

问题是因为公式:

 =CONCATIF((A1:A9=C1)*(B1:B9=D1); E1:E9) 

需要通过按CTRL + SHIFT + ENTER来调用

为了完整起见,下面是完整的Excel VBAmacros,用于连接范围内的string(可能有多个string列),如果其他列中的条件匹配。

 ' ********************************************************************** ' Modul: Modul1 Typ: Allgemeines Modul ' ********************************************************************** Option Explicit Public Function CONCATIF(Kriterium, Inhalte) Dim mydic As Object Dim L As Long Dim C As Long Dim N As Long Dim cols As Long Set mydic = CreateObject("Scripting.Dictionary") cols = Inhalte.Columns.Count N = 1 For L = 1 To UBound(Kriterium) If Kriterium(L, 1) = 1 Then For C = 1 To cols If Not IsEmpty(Inhalte(L, C)) Then mydic(N) = "* " & Inhalte(L, C) N = N + 1 End If Next End If Next CONCATIF= Join(mydic.items, vbCrLf) End Function 

使用方法如下 – 不要忘了按CTRL + SHIFT + ENTER进入

 =CONCATIF((A1:A:9="male")*(B1:B9="young"); C1:D9) 

样本数据

  ABCD 01 male young Comment 1 Comment 2 02 male old Comment 3 Comment 4 03 female young Comment 5 Comment 6 04 female old Comment 7 Comment 8 05 male young Comment 9 Comment A 

结果是

 * Comment 1 * Comment 2 * Comment 9 * Comment A 

对于Google:这也应该是VERKETTENWENN()