Long Loop

Lester601

Board Regular
Joined
Apr 4, 2003
Messages
164
Can anyone tell me why this loop takes so long with 600 emps?


For varcounter = 1 To varNbEmployees

A = ActiveCell.address
Range(A).Select
ActiveCell.Value = ActiveCell.Offset(0, 2).Value & ActiveCell.Offset(0, 3).Value & ActiveCell.Offset(0, 4).Value
ActiveCell.Offset(1, 0).Select

Next varcounter
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I don't know if it helps but you don't need to use Select.
Code:
Dim rng As Range

Set rng = ActiveCell

For varcounter = 1 To varNbEmployees

    rng.Value = rng.Offset(0, 2).Value & rng.Offset(0, 3).Value & rng.Offset(0, 4).Value
    Set rng = rng.Offset(1, 0)

Next varcounter
By the way can't you do this using worksheet functions rather than VBA?
 
Upvote 0
It think its because your selecting 600 times. Usually the less you select, the faster code runs.

Code:
iStartCol = Range("A1").Column
iStartRow = Range("A1").Row

For i = iStartRow to iStartRow+ varNbEmployees
   Cells(i, iStartCol).Value = Cells(i,iStartCol+2).Value &  Cells(i,iStartCol+3).Value & Cells(i,iStartCol+4).Value
Next i
 
Upvote 0

Forum statistics

Threads
1,203,111
Messages
6,053,571
Members
444,673
Latest member
Jagadeshrao

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