PDA

View Full Version : Solved: Update Matched Items



Petra
01-31-2007, 05:20 AM
Hi,

The code at the bottom was found after Googling ?vba to speed up MATCH in excel?.

My ?Kid-Brother?, a Computer Geek, was unable modify this code for me. He suggested I visit your site for some help.

Maybe someone would spend a few minutes looking at this?

?Update? sheet uses equations, functions & Named Ranges.
a) Match function in ?Col E? returns the row number in ?Col A? of matched item in ?Col D?
b) Index function in ?Col F? returns the data from the matched item in ?Col B? relative to ?Col E?
c) Index function in ?Col H? returns the data from the matched item in ?Col C? relative to ?Col E?
d) Named Range ?Items? for Matching
e) Named Range ?Table? for Indexing
.
The revised code below, would copy data from ?Col B? to ?Col E? and from ?Col C? to ?Col G? for each Match. As shown in the ?Modified? sheet.

Some sheets to be updated would have about 50K Items in ?Col A? and maybe 35K Items in ?Col D?


Option Explicit
Option Base 1 'ensure arrays start at 1, not 0
? Dermot Balson - January 1981
' adds up the total number of matches
' can be adapted to do something else when match found

Sub MatchArray()
Dim D1 As Variant, D2 As Variant
Dim C As New Collection
Dim i As Integer, j As Integer
Dim tCount As Integer

Dim t As Single
t = Timer 'set timer

'With ActiveSheet
With Sheets("Test")
D1 = .Range(.Cells(2, 1), .Cells(60000, 1).End(xlUp))
D2 = .Range(.Cells(2, 4), .Cells(60000, 4).End(xlUp))
End With

'store D2 in collection
For i = 1 To UBound(D2, 1)
C.Add CStr(i), CStr(D2(i, 1))
'first item is array sequence , second is string to lookup on
'both must be strings
Next i

'search
'set error trapping on
On Error Resume Next

For i = 1 To UBound(D1, 1)
'next line looks up the item from D1 in the collection, and converts the result
'to a number. If no error, gives the sequence number. So if the number is 45, it
'means B2(45,1) is a match. An error no match found.
j = Val(C(CStr(D1(i, 1))))
If Err = 0 Then 'no error, have a match
tCount = tCount + 1
Else 'no match, so reset error
Err = 0
End If
Next i

'report number of matches and time taken
t = Timer - t 'freeze timer
MsgBox "Found " & tCount & " matches in " & Format(t, "0.00") & " seconds", vbInformation, "Matched Items"
Range("cMethod") = t

End Sub


This would free-up a lot of time to use on other duties.

Thank you,

Petra

Bob Phillips
01-31-2007, 05:41 AM
Apart from the extra column in the Update sheet, I can't see the difference between Update and Modified. So what is wrong with the formulae on Update?

Petra
01-31-2007, 08:07 AM
Apart from the extra column in the Update sheet, I can't see the difference between Update and Modified. So what is wrong with the formulae on Update?

Hello Xld,

My explanation was very poorly constructed. Sorry.

Too itemize things:

a) The formulae in the Update sheet are fine.
b) It is the "Match" function that I am bogged down with.

A small list of Items (10K-15K) takes less than 10 minutes. But once the Item count gets above 30K it is slow beyond belief... plus 35 minutes to finish depending on the count.

It was my wildest hope that the code (MatchArray) could be modified to produce the same results as on the Update sheet. The Modified sheet was added as a visual aid.

I am trying to find a way, by any means, to increase the Match function speed when working with large Item lists.

Your response showed my initial foray into this site needed to be methodical. I will do better next time. :friends:

Regards,

Petra

Bob Phillips
01-31-2007, 08:14 AM
Aee you saying that a worksheet calculation takes 45 minutes? I am struggling to comprehend that, there must be many other formulae, or something else going on that is taking time. Even 10 minutes for as little as 15K is a ridiculous amount of time.

How many items will you have column D, and where do they come from (why those particular ones)?

Petra
02-01-2007, 01:56 AM
Aee you saying that a worksheet calculation takes 45 minutes? I am struggling to comprehend that, there must be many other formulae, or something else going on that is taking time. Even 10 minutes for as little as 15K is a ridiculous amount of time.

How many items will you have column D, and where do they come from (why those particular ones)?

Hello Xld,

In response to your last questions:

ColA Items are always a “Master” list of Items available for Import from various suppliers. The “Master” lists vary between suppliers..

ColD is a “Import” list of Items actually purchased and then used to update pricing based on the currency used in our transactions. The “Import” lists vary according to Sales forecasts. Which is my original file showed Cost & Sell columns for later analysis by the “Head Honchos”.

Regarding your passage “there must be many other formulae. or something else going on that is taking time”. Well,I know sometimes our system slows down, as the mainframe we rent or share sits in Ottawa.

So at home & with my cable modem disconnected... I just finished doing some Timing checks.

I created 3 workbooks (Test1, Test2, and Test3). Each workbook had one sheet, with 3 columns (A-B-C).

1. Test1 had 45,116 Items in ColA and 15,000 Items in ColB. In C2 was the Match formula ”C2=MATCH(B2,Parts,0)” and copied this down as far as ColB was populated. Hit “F9” key… it took under 3 minutes.

2. Test2 had 45,116 Items in ColA and 25,000 Items in ColB. In C2 was the Match formula ”C2=MATCH(B2,Parts,0)” and copied this down as far as ColB was populated. Hit “F9” key… it took a little over 11 minutes.

3. Test3 had 45,116 Items in ColA and 35,000 Items in ColB. In C2 was the Match formula ”C2=MATCH(B2,Parts,0)” and copied this down as far as ColB was populated. Hit “F9” key… it took a little over 29 minutes.

Would you expect these differences in time?

What am I doing wrong? :banghead:

Is there a better way? : pray2:

Regards,

Petra

Petra
02-01-2007, 07:02 PM
Hello Xld,

Think I wil move close this.

Thanks for the insight. :rotlaugh:

Regards.. Petra

Carpiem
02-03-2007, 04:18 AM
Hello Xld,

Think I wil move close this.

Thanks for the insight. :rotlaugh:

Regards.. Petra

Hi Petra,

I took your "Modified" sheet and made up 2 Tables. Using a double Index & Match arrangement, as shown, will really burn leather. A 40K table took a couple of seconds to fill in the required columns.

Xld is super smart with difficult questions & he leaves the easy ones for us. :devil2:

Ciao,

Carpiem