Home » excel » Passing an Matrix (?) to VBA in Excel

Passing an Matrix (?) to VBA in Excel

Posted by: admin April 23, 2020 Leave a comment

Questions:

one common solution in Excel to SUMIF() with multiple conditions goes like this
(this formula counts all cases in which column A has the value in C1 and column B has the value in D1):

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

I, now, need a function that concatenated strings from lines fulfilling multiple conditions. There is a great solution for one condition at http://www.ms-office-forum.net/forum/showthread.php?t=273352

I, however, want to check multiple conditions – and therefore use the SUBPRODUCT() trick from above. My problem is: How do I have to define the parameter in the function to get an Array? This is what I got so far:

' **********************************************************************
' 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

This works fine, if I select one columns for parameter 1. But as soon as I include a product formula (like above), only a Variant/Double with the value 1 is passed for 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

Any suggestions? Thank you!

How to&Answers:

The problem was because formula:

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

need to be called by pressing CTRL+SHIFT+ENTER

Answer:

For the sake of completeness, here’s the complete Excel VBA Macro to concatenate strings from a range (may have multiple string columns), if criteria in other columns are matched.

' **********************************************************************
' 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

Use as follows – and don’t forget to enter by CTRL+SHIFT+ENTER

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

Sample data

    A       B       C           D
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

Results in

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

For Google: This shall also be found as VERKETTENWENN()