Link to home
Start Free TrialLog in
Avatar of Paolo Crossi
Paolo Crossi

asked on

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
Avatar of ste5an
ste5an
Flag of Germany image

hmm, by posting that attachment?
Avatar of Paolo Crossi
Paolo Crossi

ASKER

Posted.
I have the impression if the author modifies the original question, the attachment disappears.
Anyway, now it should be at the right place.
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
Fine job.

But it is my second best.
My first choice is a vba code.
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
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?
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial