With then For Next or For Next then With

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Which is faster?

Code:
With Sheet1
    
    For counter = 1 To 100000
        
        .Cells(counter, 1).Value = counter
 
    Next counter
      
End With

or

Code:
For counter = 1 To 100000

    With Sheet1
        
        .Cells(counter, 1).Value = counter
     
    End With

Next counter

I'm inclined to think the former is faster but tested it and saw no difference.

Thanks
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
both very similar, I timed them and came out about the same. something like the below would be 10000000000000000000000000000000% quicker though

Code:
sub test()
Range("A1:A100000").Value = Evaluate("=ROW(A1:A100000)")
end sub
 
Upvote 0
Thanks, never seen your method before.

I just thought the fewer things you do inside a loop, the quicker it'll be.

I normally do this:

Code:
Dim a As Long

a = 100000

ReDim MyArray(1 To a, 1 To 1) As Variant

Dim counter As Long

For counter = 1 To a

    MyArray(counter, 1) = counter

Next counter

Sheet1.Cells(1, 1).Resize(a, 1).Value = MyArray
 
Last edited:
Upvote 0
definitely. obviously the above is a bad example as theyre the same amount of lines. the more things in a loop the worse it is. And if evaluate statements are available id use them
 
Upvote 0
Agree.

BTW, why does this not work?

Code:
Dim a As Long
a = 1000
Range("A1:A" & a).Value = Evaluate("=ROW(A1:A)" & a)

returns #VALUE instead of 1,2,3, etc.
 
Upvote 0
=ROW(A1:A)1000 is not a valid formula, and that's the string you are trying to evaluate.:)

What is it you are trying to do?
 
Upvote 0
Just trying the suggestion by BarryL but using a variable instead of a fixed row number.
 
Upvote 0
Try this then.
Code:
Dim a As Long
a = 1000
Range("A1:A" & a).Value = Evaluate("=ROW(A1:A" & a & ")")
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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