Question : VBA code is not efficient enough

Hey guys

Are there any way, that I can create more efficient code than this? It takes 13 sec. to run through the code.

Instead of sharing the excel workbook, which contains much personal information, I'll try to explain the case.

rng1 is a culumn that contains key values in a table that is 200 rows and around 120 columns.
rng2 is a culumn in another sheet, that contains some of the elements in rng1, but no elements that are not in rng1. What the code does is, it copies the values in the cells 3 and 4 columns from rng2 and pastes it into cells that are "colm" columns away from the matching elements in rng1.

Can't this be done more efficient - 13 sec is too long. Why do this take so much time?

Cheers, Raahaugen
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
Sub AAR_updateBudget()
Dim rng1 As Range
Dim rng2 As Range
Dim celle1 As Range
Dim celle2 As Range
Dim colm As Long

With Sheets("AAR_assbudget")
    Set rng1 = Range(.Cells(5, "G"), .Cells(.Rows.Count, "G").End(xlUp))
End With

With Sheets("ass_all")
Set rng2 = Range(.Cells(5, "B"), .Cells(204, "B"))
End With

For Each celle1 In rng1
    For Each celle2 In rng2
        If celle1 = celle2 Then
'_____________
            colm = Cells(2, "U").Value * 2 + 21
            celle2.Offset(0, colm) = celle1.Offset(0, 3)
'The two above lines takes 6-7 sec.
'_____________
            colm = colm + 1
            celle2.Offset(0, colm) = celle1.Offset(0, 4)
'The two above lines takes 6-7 sec.
'In total these 4 lines takes 13 sec.
'_____________
        End If
    Next celle2
Next celle1

' COD_copyformulas Makro
    Application.CutCopyMode = False
    Range("J107:K107").Copy
    Range("J5:K104").PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

End Sub

Answer : VBA code is not efficient enough

Raahaugen,

Please try the macro in the attached file - it should be a lot faster.

Patrick
Random Solutions  
 
programming4us programming4us