INDEX + MATCH; generating more rows

Martaa

New Member
Joined
Jan 20, 2023
Messages
4
Platform
  1. Windows
Hi!

I want to combine 2 tables, and by doing so I want to create a third one which has more rows than the 2 original ones.
I am doing an exercise of organizing trainings in a department.

Here is a picture of what I want to get:
1674225556056.png

As you can see, the 3rd table is the combination of the 1st and the 2nd.

How can I do that?

Thanks A LOT!!!!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Not clear to me which version is that... so I went for a User Defined Formula, that correspond to the following code:
VBA Code:
Function Summary(ByRef TB1 As Range, TB2 As Range) As Variant
Dim OArr(), oaInd As Long, cTrain As String, cDept As String
Dim W1, W2, I As Long, J As Long
'
W1 = TB1.Value
W2 = TB2.Value
'
ReDim OArr(1 To 2, 0 To 1)
For I = 1 To TB1.Rows.Count
    cTrain = W1(I, 1)
    If Len(cTrain) > 0 Then
    cDept = UCase(W1(I, 2))
    For J = 1 To TB2.Rows.Count
        If UCase(W2(J, 1)) = cDept Then
            oaInd = oaInd + 1
            ReDim Preserve OArr(1 To 2, 0 To oaInd)
            OArr(1, oaInd) = cTrain
            OArr(2, oaInd) = W2(J, 2)
        End If
    Next J
    End If
Next I
OArr(1, 0) = "Training"
OArr(2, 0) = "Name"
Summary = Application.WorksheetFunction.Transpose(OArr)
End Function
Copy the code into a standard module of your vba project.
Then return to Excel and you can use the "Summary" function, with the following syntax
Excel Formula:
Summary(Interval1, Interval2)
Interval1 is the Training /Department table; Interval2 is the Department/Name table

So, having the tables as in the figure, I used H2 the formula
Excel Formula:
=Summary(Table1,Table2)

The formula returns an array: if your XL version supports dynamic arrays then the output will expand for 2 columns and as many rows are necessary.
Otherwise you have to input the formula on a range of X rows by 2 columns as an array formula (ie: using Contr-Shift-Enter)

Try...

Cartel1
ABCDEFGHIJKL
1
2TrainingDeptDepartmentNameTrainingName=Summary(Table1,Table2)
3Event1SalesSalesJohnEvent1John
4Event2MarketingSalesSarahEvent1Sarah
5Event3HRMarketingPaulEvent2Paul
6MarketingMikeEvent2Mike
7HRAlphaEvent3Alpha
8HRBetaEvent3Beta
9HRDeltaEvent3Delta
10
11
Foglio3
Cell Formulas
RangeFormula
H2:I9H2=Summary(Table1,Table2)
Dynamic array formulas.
 

Attachments

  • MARTAA_Immagine 2023-01-20 190220.jpg
    MARTAA_Immagine 2023-01-20 190220.jpg
    69.5 KB · Views: 4
Upvote 0
Hi Anthony47,

This was of absolute great help! It works PERFECTLY, and it is exactly what I needed.
I appreciate your time and effort.

Thanks again! :D
 
Upvote 0
Not clear to me which version is that... so I went for a User Defined Formula, that correspond to the following code:
VBA Code:
Function Summary(ByRef TB1 As Range, TB2 As Range) As Variant
Dim OArr(), oaInd As Long, cTrain As String, cDept As String
Dim W1, W2, I As Long, J As Long
'
W1 = TB1.Value
W2 = TB2.Value
'
ReDim OArr(1 To 2, 0 To 1)
For I = 1 To TB1.Rows.Count
    cTrain = W1(I, 1)
    If Len(cTrain) > 0 Then
    cDept = UCase(W1(I, 2))
    For J = 1 To TB2.Rows.Count
        If UCase(W2(J, 1)) = cDept Then
            oaInd = oaInd + 1
            ReDim Preserve OArr(1 To 2, 0 To oaInd)
            OArr(1, oaInd) = cTrain
            OArr(2, oaInd) = W2(J, 2)
        End If
    Next J
    End If
Next I
OArr(1, 0) = "Training"
OArr(2, 0) = "Name"
Summary = Application.WorksheetFunction.Transpose(OArr)
End Function
Copy the code into a standard module of your vba project.
Then return to Excel and you can use the "Summary" function, with the following syntax
Excel Formula:
Summary(Interval1, Interval2)
Interval1 is the Training /Department table; Interval2 is the Department/Name table

So, having the tables as in the figure, I used H2 the formula
Excel Formula:
=Summary(Table1,Table2)

The formula returns an array: if your XL version supports dynamic arrays then the output will expand for 2 columns and as many rows are necessary.
Otherwise you have to input the formula on a range of X rows by 2 columns as an array formula (ie: using Contr-Shift-Enter)

Try...

Cartel1
ABCDEFGHIJKL
1
2TrainingDeptDepartmentNameTrainingName=Summary(Table1,Table2)
3Event1SalesSalesJohnEvent1John
4Event2MarketingSalesSarahEvent1Sarah
5Event3HRMarketingPaulEvent2Paul
6MarketingMikeEvent2Mike
7HRAlphaEvent3Alpha
8HRBetaEvent3Beta
9HRDeltaEvent3Delta
10
11
Foglio3
Cell Formulas
RangeFormula
H2:I9H2=Summary(Table1,Table2)
Dynamic array formulas.
Hi Anthony47,

As mentioned in the last comment, your suggestion works perfectly! Thanks A LOT!

I would like to add 2 more features to your code though (2 columns painted in blue in the following picture), and I was wondering if you have a clue on how to do it.

I would like to add a 3rd column to the output, being the department each person is in (see column J in the picture), and a 4th column which is the attendance (see column K).
This column indicates if a person has attended the training they were assigned or not, depending on the attendance list, which is painted in yellow.

1674462220710.png

Thanks again!

Marta
 
Upvote 0
In this case the code for the function changes to:
VBA Code:
Function SummaryXA(ByRef TB1 As Range, TB2 As Range, ByRef Attend As Range) As Variant
Dim OArr(), oaInd As Long, cTrain As String, cDept As String
Dim W1, W2, I As Long, J As Long
'
W1 = TB1.Value
W2 = TB2.Value
'
ReDim OArr(1 To 4, 0 To 1)
For I = 1 To TB1.Rows.Count
    cTrain = W1(I, 1)
    If Len(cTrain) > 0 Then
    cDept = UCase(W1(I, 2))
    For J = 1 To TB2.Rows.Count
    Debug.Print "J=" & J
        If UCase(W2(J, 1)) = cDept Then
            oaInd = oaInd + 1
            ReDim Preserve OArr(1 To 4, 0 To oaInd)
            OArr(1, oaInd) = cTrain
            OArr(2, oaInd) = W2(J, 2)
            OArr(3, oaInd) = W2(J, 1)
            OArr(4, oaInd) = "No"
            For k = 1 To Attend.Rows.Column
                If Attend.Cells(k, 1) = cTrain And Attend.Cells(k, 2) = W2(J, 2) Then
                    OArr(4, oaInd) = "Yes"
                    Exit For
                End If
            Next k
        End If
    Next J
    End If
Next I
OArr(1, 0) = "Training"
OArr(2, 0) = "Name"
OArr(3, 0) = "Dept"
OArr(4, 0) = "Attended"
SummaryXA = Application.WorksheetFunction.Transpose(OArr)
End Function
The syntax:
Excel Formula:
 SummaryXA(Interval1, Interval2, Interval3)
So I used, in H1:
VBA Code:
=SummaryXA(Table1,Table2,M2:N20)

Please update your profile to specify which Office version you use (eg 2019, 365, 2021,...)

MULTI_C30121.xlsm
ABCDEFGHIJKLMN
1Attendance list
2TrainingDeptDepartmentNameTrainingNameDeptAttendedTrainingAttendance
3Event1SalesSalesJohnEvent1JohnSalesYesEvent1John
4Event2MarketingSalesSarahEvent1SarahSalesYesEvent1Sarah
5Event3HRMarketingPaulEvent2PaulMarketingYesEvent2Paul
6MarketingMikeEvent2MikeMarketingNoEvent3Alpha
7HRAlphaEvent3AlphaHRYesEvent3Delta
8HRBetaEvent3BetaHRNo
9HRDeltaEvent3DeltaHRYes
10
11
12
13
Foglio3
Cell Formulas
RangeFormula
H2:K9H2=SummaryXA(Table1,Table2,M2:N20)
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top