Make this code run quicker to overcome its lag

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
I have the following code but a bit long winded.
Can you please advise how it can be simplified to run smoother / lag free.
Thanks

Rich (BB code):
Private Sub PasteIfFormulas_Click()
     Dim WS As Worksheet

     Set WS = Worksheets("INV")

     WS.Range("G14").Formula = "=IFERROR(IF(INDEX(DATABASE!R:R,$H$13)=0,"""",INDEX(DATABASE!R:R,$H$13)),"""")"
     WS.Range("G14").Formula = "=IFERROR(IF(INDEX(DATABASE!R:R,$H$13)=0,"""",INDEX(DATABASE!R:R,$H$13)),"""")"
     WS.Range("G15").Formula = "=IFERROR(IF(INDEX(DATABASE!S:S,$H$13)=0,"""",INDEX(DATABASE!S:S,$H$13)),"""")"
     WS.Range("G16").Formula = "=IFERROR(IF(INDEX(DATABASE!T:T,$H$13)=0,"""",INDEX(DATABASE!T:T,$H$13)),"""")"
     WS.Range("G17").Formula = "=IFERROR(IF(INDEX(DATABASE!U:U,$H$13)=0,"""",INDEX(DATABASE!U:U,$H$13)),"""")"
     WS.Range("G18").Formula = "=IFERROR(IF(INDEX(DATABASE!V:V,$H$13)=0,"""",INDEX(DATABASE!V:V,$H$13)),"""")"
     
     WS.Range("L14").Formula = "=IFERROR(IF(INDEX(DATABASE!D:D,$H$13)=0,"""",INDEX(DATABASE!D:D,$H$13)),"""")"
     WS.Range("L15").Formula = "=IFERROR(IF(INDEX(DATABASE!B:B,$H$13)=0,"""",INDEX(DATABASE!B:B,$H$13)),"""")"
     WS.Range("L16").Formula = "=IFERROR(IF(INDEX(DATABASE!L:L,$H$13)=0,"""",INDEX(DATABASE!L:L,$H$13)),"""")"
     WS.Range("L17").Formula = "=IFERROR(IF(INDEX(DATABASE!W:W,$H$13)=0,"""",INDEX(DATABASE!W:W,$H$13)),"""")"
     
     WS.Range("M27").Formula = "=IF(AND(H27="""",H27=""""),"""",H27*J27)"
     WS.Range("M28").Formula = "=IF(AND(H28="""",H28=""""),"""",H28*J28)"
     WS.Range("M29").Formula = "=IF(AND(H29="""",H29=""""),"""",H29*J29)"
     WS.Range("M30").Formula = "=IF(AND(H30="""",H30=""""),"""",H30*J30)"
     WS.Range("M31").Formula = "=IF(AND(H31="""",H31=""""),"""",H31*J31)"
     WS.Range("M32").Formula = "=IF(AND(H32="""",H32=""""),"""",H32*J32)"
     WS.Range("M33").Formula = "=IF(AND(H33="""",H33=""""),"""",H33*J33)"
     WS.Range("M34").Formula = "=IF(AND(H34="""",H34=""""),"""",H34*J34)"
     WS.Range("M35").Formula = "=IF(AND(H35="""",H35=""""),"""",H35*J35)"
     WS.Range("M36").Formula = "=IF(AND(H36="""",H36=""""),"""",H36*J36)"
     
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
See if this helps:
VBA Code:
Private Sub PasteIfFormulas_Click()
     Dim WS As Worksheet

     Application.ScreenUpdating = False
     Application.Calculation = xlCalculationManual

     Set WS = Worksheets("INV")

     WS.Range("G14").Formula = "=IFERROR(IF(INDEX(DATABASE!R:R,$H$13)=0,"""",INDEX(DATABASE!R:R,$H$13)),"""")"
     WS.Range("G15").Formula = "=IFERROR(IF(INDEX(DATABASE!S:S,$H$13)=0,"""",INDEX(DATABASE!S:S,$H$13)),"""")"
     WS.Range("G16").Formula = "=IFERROR(IF(INDEX(DATABASE!T:T,$H$13)=0,"""",INDEX(DATABASE!T:T,$H$13)),"""")"
     WS.Range("G17").Formula = "=IFERROR(IF(INDEX(DATABASE!U:U,$H$13)=0,"""",INDEX(DATABASE!U:U,$H$13)),"""")"
     WS.Range("G18").Formula = "=IFERROR(IF(INDEX(DATABASE!V:V,$H$13)=0,"""",INDEX(DATABASE!V:V,$H$13)),"""")"
     
     WS.Range("L14").Formula = "=IFERROR(IF(INDEX(DATABASE!D:D,$H$13)=0,"""",INDEX(DATABASE!D:D,$H$13)),"""")"
     WS.Range("L15").Formula = "=IFERROR(IF(INDEX(DATABASE!B:B,$H$13)=0,"""",INDEX(DATABASE!B:B,$H$13)),"""")"
     WS.Range("L16").Formula = "=IFERROR(IF(INDEX(DATABASE!L:L,$H$13)=0,"""",INDEX(DATABASE!L:L,$H$13)),"""")"
     WS.Range("L17").Formula = "=IFERROR(IF(INDEX(DATABASE!W:W,$H$13)=0,"""",INDEX(DATABASE!W:W,$H$13)),"""")"
     
     WS.Range("M27:M36").Formula = "=IF(AND(H27="""",H27=""""),"""
     
     Application.Calculation = xlCalculationAutomatic
     Application.ScreenUpdating = True
     
End Sub
 
Upvote 0
Hi,
I get a RTE 1004
application defind error.

This line in yellow

Rich (BB code):
WS.Range("M27:M36").Formula = "=IF(AND(H27="""",H27=""""),"""
 
Upvote 0
With that line removed i then run the code & no RTe shown.
So something that line doesnt like
 
Upvote 0
You can change that part abck to what you had originally:
Rich (BB code):
Private Sub PasteIfFormulas_Click()
     Dim WS As Worksheet

     Application.ScreenUpdating = False
     Application.Calculation = xlCalculationManual

     Set WS = Worksheets("INV")

     WS.Range("G14").Formula = "=IFERROR(IF(INDEX(DATABASE!R:R,$H$13)=0,"""",INDEX(DATABASE!R:R,$H$13)),"""")"
     WS.Range("G15").Formula = "=IFERROR(IF(INDEX(DATABASE!S:S,$H$13)=0,"""",INDEX(DATABASE!S:S,$H$13)),"""")"
     WS.Range("G16").Formula = "=IFERROR(IF(INDEX(DATABASE!T:T,$H$13)=0,"""",INDEX(DATABASE!T:T,$H$13)),"""")"
     WS.Range("G17").Formula = "=IFERROR(IF(INDEX(DATABASE!U:U,$H$13)=0,"""",INDEX(DATABASE!U:U,$H$13)),"""")"
     WS.Range("G18").Formula = "=IFERROR(IF(INDEX(DATABASE!V:V,$H$13)=0,"""",INDEX(DATABASE!V:V,$H$13)),"""")"
     
     WS.Range("L14").Formula = "=IFERROR(IF(INDEX(DATABASE!D:D,$H$13)=0,"""",INDEX(DATABASE!D:D,$H$13)),"""")"
     WS.Range("L15").Formula = "=IFERROR(IF(INDEX(DATABASE!B:B,$H$13)=0,"""",INDEX(DATABASE!B:B,$H$13)),"""")"
     WS.Range("L16").Formula = "=IFERROR(IF(INDEX(DATABASE!L:L,$H$13)=0,"""",INDEX(DATABASE!L:L,$H$13)),"""")"
     WS.Range("L17").Formula = "=IFERROR(IF(INDEX(DATABASE!W:W,$H$13)=0,"""",INDEX(DATABASE!W:W,$H$13)),"""")"
     
     WS.Range("M27").Formula = "=IF(AND(H27="""",H27=""""),"""",H27*J27)"
     WS.Range("M28").Formula = "=IF(AND(H28="""",H28=""""),"""",H28*J28)"
     WS.Range("M29").Formula = "=IF(AND(H29="""",H29=""""),"""",H29*J29)"
     WS.Range("M30").Formula = "=IF(AND(H30="""",H30=""""),"""",H30*J30)"
     WS.Range("M31").Formula = "=IF(AND(H31="""",H31=""""),"""",H31*J31)"
     WS.Range("M32").Formula = "=IF(AND(H32="""",H32=""""),"""",H32*J32)"
     WS.Range("M33").Formula = "=IF(AND(H33="""",H33=""""),"""",H33*J33)"
     WS.Range("M34").Formula = "=IF(AND(H34="""",H34=""""),"""",H34*J34)"
     WS.Range("M35").Formula = "=IF(AND(H35="""",H35=""""),"""",H35*J35)"
     WS.Range("M36").Formula = "=IF(AND(H36="""",H36=""""),"""",H36*J36)"
     
     Application.Calculation = xlCalculationAutomatic
     Application.ScreenUpdating = True
     
End Sub
The important parts are the lines I added highlighted in red.
They should speed up the code.
 
Upvote 0
Solution
Hi,
Yes it was but just advising the outcome to confirm it

Thanks
 
Upvote 0
Hi,
Yes it was but just advising the outcome to confirm it

Thanks
No problem.
Your wording on that reply was just a little strange.
"By the way" seems to imply that it was an unexpected, secondary outcome, when that was the goal of my code all along (as I thought that is precisely what you were asking for).
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top