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

Paolo Crossi
Paolo Crossi used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
hmm, by posting that attachment?
Paolo CrossiAdministrative employee

Author

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.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Paolo CrossiAdministrative employee

Author

Commented:
Fine job.

But it is my second best.
My first choice is a vba code.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
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
Paolo CrossiAdministrative employee

Author

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?
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial