Module running extremely slowly

andygame

New Member
Joined
May 15, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi All - I have a spreadsheet table containing around 7000 rows of data. Each row contains details about a Healthcare worker and a visit they have undertaken to a Client. For each of these visits (rows) I need to calculate their respective rate of pay. Various criteria define what pay rate is applicable - such as County, weekday or weekend, Banks holiday etc. The module I am running works perfectly up until the point where it has to write the data into the table. I am running a loop sequence which checks all the relevant information on each row and then evaluates what rate is applicable. If I run the module without trying to write the data back to the table it runs in under a second. However, as soon as I introduce the following line of code as the last line in the loop, the run time becomes minutes rather than fractions of a second.
Range("M" & n).Value = rate * Range("E" & n).value Where `M' is the column reference of where I am putting the data within the table. `n' is the variable used in the loop. `rate' is the variable which is calculated for each row as being the hourly rate to be paid. `E' is a time expressed as a fraction (ie. 30 mins would become 0.5) I have done all the obvious things like turning off screen updating etc but just cannot work out why it runs so slowly as soon as I want to add this final line. I am hoping there is an obvious solution. At present, I haven't added the entire sheet and module to this post for Data Protection reasons. I am fairly new to writing VBA code, so I can only assume that either the line of code above is causing the problem or there is something about using VBA to write data into a table that I don't yet understand. Any help or light you can throw on the problem would be really helpful.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
It is generally not a good idea to write to one cell at a time. If you are populating all (or many) cells in a column for example, populate an array with the values and then write that to the column in one go at the end.
 
Upvote 0
Solution
It is generally not a good idea to write to one cell at a time. If you are populating all (or many) cells in a column for example, populate an array with the values and then write that to the column in one go at the end.
Thanks Rory - will give that a try and let you know how I get on.
 
Upvote 0
It is generally not a good idea to write to one cell at a time. If you are populating all (or many) cells in a column for example, populate an array with the values and then write that to the column in one go at the end.
Hi Rory - Brilliant. Stuck all the results in an array as you suggested and then pasted the whole lot out in one line of code. Under 2 seconds to run the whole thing. Many thanks
 
Upvote 0
Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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