PDA

View Full Version : Solved: My seemingly simply code is VERY slow



zyffe
03-14-2013, 08:41 AM
Hi there. I'm new to this forum, so please go easy on me if the information provided is is somewhat incomplete.

I use Excel 2007

In the sheet “results” I have a list of company deals.
For each Target company (the one which has been bought by the Acquirer) there is a unique “Target BvD ID number” which we can use to match data between the two sheets “results” (my main sheet) and “data to incorporate” (the sheet from where I wish to import pieces of data).
In other words, I simply want the VBA code to take the 12 colums of data (for each unique “Target BvD ID number”) from the sheet called “Data to incorporate”, and put it where it belongs in the sheet called “results” (matched by the right Id number).

The Problem: Now, the code actually does what its meant to, but it is really slow. After half an hour, it wasn't even half way through the task. How do i speed it up?

If it is possible, i would like to attach the Excel file..

Sub Dataimport()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Dataimport(4000, 13) As Variant
'Data regarding the Target company is gathered
For i = 1 To 4000
For j = 1 To 13
Dataimport(i, j) = Sheets("Data to incorporate").Cells(i + 2, j)
Next j
Next i
'The imported data is matched with the data in the sheet "results"
For j = 1 To 4000
For i = 1 To 4000
For h = 1 To 12
If Sheets("Results").Cells(j + 3, 3) = Dataimport(i, 1) Then Sheets("Results").Cells(j + 3, h + 16) = Dataimport(i, h + 1)
Next h
Next i
Next j
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub


Cheers

enrand22
03-14-2013, 09:18 AM
your code is ok but you are doing 192 052 000 calculations, may be if you get rid of some "For" by using formulas or making more elaborate code, you will feel it faster.

i dont think some minor tweaks in your code could faster it.

zyffe
03-14-2013, 09:25 AM
Thanks for the reply. I am really inexperienced in this VBA game however. How could i "use formulas" instead?

enrand22
03-14-2013, 09:30 AM
may be it you attach some example... i can help you better... for now i can tell that you have access to spreadsheet functionskike application.worksheetfunctions.sum or application.worksheetfunctions.vlookup if used properly code will be more efficient

zyffe
03-14-2013, 09:52 AM
I need 5 posts to be able to post a link

This is #3

zyffe
03-14-2013, 09:53 AM
I need 5 posts to be able to post a link

This is #4

zyffe
03-14-2013, 09:54 AM
I need 5 posts to be able to post a link

This is #5

zyffe
03-14-2013, 09:55 AM
I have uploadet the excel file here http://gratisupload.dk/vis/693374 That way you can see what i am talking about

just click "KLIK HER FOR AT DOWNLOADE" <-- Sorry for Danish :D

zyffe
03-14-2013, 10:08 AM
For the record - I am trying to work in some formulas meanwhile. But, i can't seem to make it work

enrand22
03-14-2013, 10:22 AM
with this macro it took 30 seconds to finish


Sub zyffe()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False


Range("Q4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C2,2,FALSE)"
Range("R4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C3,3,FALSE)"
Range("S4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C4,4,FALSE)"
Range("T4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C5,5,FALSE)"
Range("U4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C6,6,FALSE)"
Range("V4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C7,7,FALSE)"
Range("W4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C8,8,FALSE)"
Range("X4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C9,9,FALSE)"
Range("Y4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C10,10,FALSE)"
Range("Z4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C11,11,FALSE)"
Range("AA4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C12,12,FALSE)"
Range("AB4").FormulaR1C1 = "=VLOOKUP(RC3,'Data to incorporate'!C1:C13,13,FALSE)"
Range("Q4:AB4").AutoFill Destination:=Range("Q4:AB2794"), Type:=xlFillDefault


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

zyffe
03-14-2013, 11:29 AM
I certainly does. Thanks alot for the help :D

zyffe
03-14-2013, 11:30 AM
By the way.. Can i give you points for helping out, or how does it work?

enrand22
03-14-2013, 11:31 AM
perfect! you are welcome!

enrand22
03-14-2013, 11:33 AM
mmmm no... i think it is just altruism or i dont really know, i begin here on monday too, to practice my coding.