Excel VBA VLOOKUP with 2 criterias with parameters in rows

sofia_k

New Member
Joined
Apr 24, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I need help with VLOOKUP in VBA that i havent figured out for weeks now (but im still a beginner too).

I have file with 2 sheets - data and calculation.
Data sheet has base data as column A has metric name, columns B to O has values
but the catch is that criteria values are in rows 1 and 2 - date and country
fe.
A B C D
01.11.2023 01.12.2023 01.01.2024
NO FI CA
Total salary 520835 4220170 7187268
GGR 112004 529309 334859

Sheet calculation has dates in column A, countries in Column B and metrics in first row of columns C to J
A B C D
DATE COUNTRY Total salary GGR
01.11.2023 NO
01.12.2023 FI

So i would need it to find fe. total salary value to calculation sheet (C2) based on date and country given in columns a and b and to loop it until the end of values given in columns a/b. I couldnt find any version where vloopup would search from rows as criteria, also i couldnt make index match work so help!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi @sofia_k. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

If I understand correctly, your data in the DATA sheet is like this:
Dante Amor
ABCD
101.11.202301.12.202301.01.2024
2NOFICA
3Total salary520,835.004,220,170.007,187,268.00
4GGR112004529309334859
Data

So with the SUMPRODUCT function, you can get the results in the calculation sheet, like this:
Dante Amor
ABCD
1DATECOUNTRYTotal salaryGGR
201.11.2023NO520,835.00112004
301.12.2023FI4,220,170.00529309
401.01.2024CA7,187,268.00334859
calculation
Cell Formulas
RangeFormula
C2:C4C2=SUMPRODUCT((Data!$B$1:$O$1=$A2)*(Data!$B$2:$O$2=$B2)*(Data!$B$3:$O$3))
D2:D4D2=SUMPRODUCT((Data!$B$1:$O$1=$A2)*(Data!$B$2:$O$2=$B2)*(Data!$B$4:$O$4))

If you want the macro in VBA then it could be like this:
VBA Code:
Sub vlookup2criteras()
  With Sheets("calculation").Range("C2:C" & Sheets("calculation").Range("A" & Rows.Count).End(3).Row)
    .Formula = "=SUMPRODUCT((Data!$B$1:$O$1=$A2)*(Data!$B$2:$O$2=$B2)*(Data!$B$3:$O$3))"
    .Value = .Value
  End With
  With Sheets("calculation").Range("D2:D" & Sheets("calculation").Range("A" & Rows.Count).End(3).Row)
    .Formula = "=SUMPRODUCT((Data!$B$1:$O$1=$A2)*(Data!$B$2:$O$2=$B2)*(Data!$B$4:$O$4))"
    .Value = .Value
  End With
End Sub
---------------
Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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