VBA and Excel: how to fill a table matching rows and columns?

Hello everybody.

I have to fill the area C2:E11 - which actually is a table - with the data in the column L. The criteria of filling are set by the columns I:K.

As you can see in the attachment, the value in cell L2 (7) has to be reported in date 29/9/18 related to the pair BBBBB (column j) and XXX (column K) ---> cell D6
And so on for any of the value in column L until L6.

How can I figure it out?
Tabel1.xlsx
Paolo CrossiAdministrative employeeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, by posting that attachment?
0
Paolo CrossiAdministrative employeeAuthor Commented:
Posted.
I have the impression if the author modifies the original question, the attachment disappears.
Anyway, now it should be at the right place.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Try this...

In C2
=IFERROR(INDEX($L$2:$L$6,MATCH(1,INDEX(($J$2:$J$6=$A2)*($K$2:$K$6=$B2)*($I$2:$I$6=C$1),),0)),"")

Open in new window

and then copy across and down.
Tabel1.xlsx
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Paolo CrossiAdministrative employeeAuthor Commented:
Fine job.

But it is my second best.
My first choice is a vba code.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
If you don't want to place the formulas manually, you can use VBA to place the formulas and convert them into values.
In the attached, click the button called "Fill Table" to run the code.

Sub FillTable()
Dim ws As Worksheet
Set ws = Sheets("Foglio1")
With ws.Range("C2:E11")
    .Formula = "=IFERROR(INDEX($L$2:$L$6,MATCH(1,INDEX(($J$2:$J$6=$A2)*($K$2:$K$6=$B2)*($I$2:$I$6=C$1),),0)),"""")"
    .Value = .Value
End With
End Sub

Open in new window

Tabel1.xlsm
0
Paolo CrossiAdministrative employeeAuthor Commented:
Well.

But in real life, I have variables to manage.

The range to fill is:

sheet1.Range(Cells(2, 4), Cells(lr, data))

Open in new window


The range to take data is:

sheet2.Range("AI2:AL" & lr2)

Open in new window


How can I adapt your solution to the different range sizes?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Based on your input, I have hard coded the variables based on the data in the sample file. Tweak it as per your requirement.

Sub FillTable()
Dim data As Long, lr As Long, lr2 As Long
lr = 11
data = 6
lr2 = 6
With Sheet1.Range(Cells(2, 4), Cells(lr, data))
    .Formula = "=IFERROR(INDEX('" & Sheet2.Name & "'!$AL$2:$AL$" & lr2 & ",MATCH(1,INDEX(('" & Sheet2.Name & "'!$AJ$2:$AJ$" & lr2 & "=$A2)*('" & Sheet2.Name & "'!$AK$2:$AK$" & lr2 & "=$B2)*('" & Sheet2.Name & "'!$AI$2:$AI$" & lr2 & "=D$1),),0)),"""")"
    '.Value = .Value
End With
End Sub

Open in new window

I have commented out the line#8 so that you can view what formula is being placed in the range and it would be easy for you to tweak the formula based on your actual criteria columns on sheet1. After tweaking the formula correctly in the VBA code, you can uncomment the line#8 by removing the single quote from the beginning of that line so that the formulas will be converted to their values in the end.

To run the code, just click the button called "Fill Data".
Tabel1-v2.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.