[EXCEL VBA] Help - Datafilling 1 wks from another (need a fast method)
I've got a massive Excel VBA 'macro' that in the middle of running ends up with two worksheets of data. These two sheets are farily large, containing up to 5,000 rows of data on the largest.
'-------made up example-------------
sheet 1 contains the following column names:
Car Name, Car Make, Car Model, Car Color, Car Size, OWNER
sheet 2 contains only one matching column:
OWNER, address, phone number, sex, age, height weight.
note: Sheet 2 will have all unique records, but an OWNER name may show up 50 times.
'-----------------------------------------
'------What I have to do -------------
What I have to down is:
A) Insert some columns into sheet 2 (for every column except "OWNER" that exists in Sheet 1)
B) datafill those extra columns with the data from sheet 1
'--------------------------------------------
'------How I'm doing it now -------------
A) I create some arrays and dim then by the number of rows in Sheet 1 (minus 1 to account for the column headers on row1).
B) I load those arrays with the data from Sheet 1
C) I go down ever row in Sheet 2 and if Owner matches I dump the array data in to the appropriate cells.
'------------------------------------------------
'----How I do it now -------------
'--note: made up on the fly
'--note: The real routine has about 20 'arrays' for all the columns
Code:
Redim MyArray1(LastRow - 1)
Redim MyArray2(LastRow -2)
Sheet1.activate
For X = 2 to LastRow
MyArray1(X-1) = Cells(X,1).value
MyArray2(X-1) = Cells(X,2).value
Next X
Sheet2.activate
For X = 2 to LastRow
For Y = 1 to Sheet1.LastRow - 1
If Cells(X,3).value = MyArray1(Y) Then
Cells(X,4).value = MyArray2(Y)
Exit For
End If
Next Y
Next X
'-----------------------------------------------------------------------------
'-------What I'm looking for ---------------------
That method is so freaking slow it makes me cry.
I barley know jack about Excel forumlas and even less about how to use them from VBA.
With the forumlas or some other method, what is the fastest way that I can datafill sheet2 with the data from sheet1?
'--------------------------------------------------------
THANKS