VBA code running slow after specific code was executed

Roy_Excel_Island_Apps

Board Regular
Joined
Oct 9, 2018
Messages
76
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

something weird is happening in my workbook code. I've been searching for hours what was slowing down the process:

1. I run my code when I've opened the workbook: 0 seconds to execute it.
2. I run my code a second time: Going 1 record per second (And I have 10K records...), so VERY SLOW
3. While running I break the execution and click 'End' so the macro stops.
4. I run it again: 0 seconds!
5. Step 2 happens again...
6. Loop..

I had a look at what is causing the code going slow at the second time you run it and this is the code:

' Set rng = shtOUTPUT.Range("A2:A" & LR)
' shtOUTPUT_Template.Range("H2").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
'
' Set rng = shtOUTPUT.Range("B1:BE" & LR)
' shtOUTPUT_Template.Range("N1").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value

' Set rng = Nothing

When I put the code above in comments, the code is running FAST EVERY EXECUTION. So I am sure this code is slowing it down...BUT I DON'T KNOW WHY...
Can somebody tell me why??? Thanks in advance!

Regards, Roy
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Does the value of LR change between the fast & slow runs?
 
Upvote 0
Does the value of LR change between the fast & slow runs?

LR = shtOUTPUT.Cells(shtOUTPUT.Rows.Count, "A").End(xlUp).Row

So yes. It counts the last row. This is always the same because I clear the sheet in the beginning of the execution: 'shtOUTPUT.Rows("2:" & Rows.Count).ClearContents'
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,837
Members
449,193
Latest member
MikeVol

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