I am using this little code which takes nearly 250ms to run: (i
is the increment of a loop on lines)
Sheets("sheet1").Select
val1 = Sheets("sheet2").Range("D" & Sheets("sheet1").Range("A" & i).Value + 1)
val2 = Sheets("sheet2").Range("E" & Sheets("sheet1").Range("A" & i).Value + 1)
Sheets("sheet1").Range("G" & i).Value = val1
Sheets("sheet1").Range("O" & i).Value = val2
I am not sure why this is taking so long for now.
Is it because I move data from one sheet to another? Maybe using a variant
will be faster?
Do you have any pointers on what could I do to make it faster? (this loop is done 300-400 times so the total time is too long.)
(not sure if this has a better place here or in "code reviews", let me know if I am wrong)
Thank you for your help!
[EDIT]
Here is the loop after your suggested changes, thank you all, that reduced the time spent by 30%.
However it is still too long for me, I have noted your suggestion to copy all the sheets sh1
and sh2
in a table, I will try to implement that. If you have other ideas, please keep me posted =)
i = 2
While sh1.Cells(i, 1).Value <> ""
val5 = sh1.Cells(i, 1).Value2 + 1
With sh2
val3 = .Cells(val5, 4).Value2
val4 = .Cells(val5, 5).Value2
End With
With sh1
.Cells(i, 7).Value2 = val3
.Cells(i, 15).Value2 = val4
End With
i = i + 1
Wend