How to perform Index-match with great amount of rows (formula or VBA)

ffariagui

New Member
Joined
Mar 30, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi everyone. I hope y're all fine.
I have a spreedsheet with prices for many products, according with the 4 criteria bellow:

(1) City (2) Seller (3) Date (4) Product type

Each product has a product "below" which price must be lower. So for example, the product B must be more expensive than product A, considering same City, Seller and Date. Basically i have to compare prices between products B and A to see if this rule is being obeyed. I'm trying to perform an index-match to fill a new column (Column J) so i can compare the prices, and do some calculation over it.
With worksheet formulas this task become very inefficient when the spreadsheet exceeds 500k rows
I also tried to use arrays in VBA (code below) to fill, but it also proved to be inefficient.

Do you guys have some tips on this problem?


Array_code:

Sub IndexMatch()

Dim arr As Variant
Dim i As Long
Dim j As Long
Dim StartTime As Single

StartTime = Timer

arr = Range("C4").CurrentRegion

For i = (LBound(arr, 1) + 1) To UBound(arr, 1)

For j = (LBound(arr, 1) + 1) To UBound(arr, 1)

If arr(j, 10) = arr(i, 11) Then
arr(i, 8) = arr(j, 5)
Exit For

End If
Next j
Next i


MsgBox (Timer - StartTime)


Price Calculation.xlsb
ABCDEFGHIJKLMNOPQRS
1
2Formulas:#N/D#N/D #N/D
3
4CitySellerDateProductPrice.Product BelowPrice Product Below.Key_1Key_2ProductProduct Below
5City1Seller131/03/2022A194NoneNoneCity1Seller144651ACity1Seller144651-ANone
6City1Seller131/03/2022B190A194City1Seller144651BCity1Seller144651ABA
7City1Seller131/03/2022C169A194City1Seller144651CCity1Seller144651ACA
8City1Seller131/03/2022D161C169City1Seller144651DCity1Seller144651CDC
9City1Seller131/03/2022E199C169City1Seller144651ECity1Seller144651CEC
10City1Seller131/03/2022F158D161City1Seller144651FCity1Seller144651DFD
11City1Seller131/03/2022G188D161City1Seller144651GCity1Seller144651DGD
12City1Seller131/03/2022H110F158City1Seller144651HCity1Seller144651FHF
13City1Seller131/03/2022I157G188City1Seller144651ICity1Seller144651GIG
14City1Seller131/03/2022J121G188City1Seller144651JCity1Seller144651GJG
15City1Seller131/03/2022K163G188City1Seller144651KCity1Seller144651GKG
16City1Seller131/03/2022L131I157City1Seller144651LCity1Seller144651ILI
17City1Seller131/03/2022M106J121City1Seller144651MCity1Seller144651JMJ
18City1Seller131/03/2022N128J121City1Seller144651NCity1Seller144651JNJ
19City1Seller131/03/2022O198L131City1Seller144651OCity1Seller144651LOL
20City1Seller131/03/2022P189N128City1Seller144651PCity1Seller144651NPN
21City1Seller131/03/2022Q191M106City1Seller144651QCity1Seller144651MQM
22City1Seller131/03/2022R152L131City1Seller144651RCity1Seller144651LRL
23City1Seller131/03/2022S125L131City1Seller144651SCity1Seller144651LSL
24City1Seller131/03/2022T189Q191City1Seller144651TCity1Seller144651QTQ
25City1Seller131/03/2022U157Q191City1Seller144651UCity1Seller144651QUQ
26City1Seller131/03/2022V116U157City1Seller144651VCity1Seller144651UVU
27City1Seller131/03/2022W116Q191City1Seller144651WCity1Seller144651QWQ
28City1Seller131/03/2022X111V116City1Seller144651XCity1Seller144651VXV
29City1Seller131/03/2022Y174X111City1Seller144651YCity1Seller144651XYX
30City1Seller131/03/2022Z153X111City1Seller144651ZCity1Seller144651XZX
Calculation
Cell Formulas
RangeFormula
I2I2=INDEX($R$5:$R$30,MATCH(F3,$Q$5:$Q$30,0))
J2J2=IF(I2="None","None",INDEX(G:G,MATCH(M2,L:L,0)))
L2L2=$C3&$D3&$E3&$F3
M2M2=$C3&$D3&$E3&$I2
 

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.
Have a look a scripting dictionaries it might be faster to use them rather than a double loop through a large array.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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