vba for next

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
985
Office Version
  1. 2010
Platform
  1. Windows
Hello People.
VBA Code:
Sub Monte_Carlo()

Dim i As Integer

         For i = 2 To 16
                 Cells(i, 1) = i + Range("A1").Value - 2
         Next

End Sub
1618104958928.png

This is the result I got, but it is not what I am working on.
Expecting result is:
1618105117691.png

Please give me a hand here.
Thank you for reading this.
 
I disagree. Looping can be very useful in some circumstances.
Here the OP has indicated (post #7) that the actual range is bigger than the example in post #1 so I tested with 150 rows and 50 columns and expanded the code from post #5 to that range.
Over a series of runs the average run time on my machine was 0.062 seconds
With the following double loop code the same results were achieved in an average of 0.022 seconds. That is, in about 35% of the time.

VBA Code:
Sub Demo02()
  Dim a(1 To 150, 1 To 50) As Long
  Dim c As Long, r As Long, currval As Long

  currval = Range("A1").Value
  For c = 1 To UBound(a, 2)
    For r = 1 To UBound(a)
      a(r, c) = currval
      currval = currval + 1
    Next r
  Next c
  Range("A2").Resize(UBound(a), UBound(a, 2)).Value = a
End Sub


BTW, the non-looping version from post #5 could be made more than twice as fast (by my testing on this larger range) as follows - but still not as fast as the looping code above.

VBA Code:
Sub Demo03()
    With [A2:AX151]:  .Value = Evaluate("A1+ROW(" & .Address & ")-2+(COLUMN(" & .Address & ")-1)*150"):  End With
End Sub



How is that an example when you have not provided those code lines?
In any case, I don't think the value of any code should be measured by how many lines it takes.
Agree.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Peter_SSs Thank you so much, your code work perfect, beside use an array, great. big lesson for me, You are great teacher.
 
Upvote 0
You're welcome. Glad to contribute. Thanks for the follow-up. :)
 
Upvote 0
As I wrote in post #15 'useless' so between quotes …​
As I wrote in post #18, in particular for beginners, it can make a code easier just thinking first about what Excel offers​
rather than following a classic algorithm path …​
Obviously for big data playing with arrays and loops is often the way to go.​
But for few data, what is the best code, the faster, the shorter, the smarter, the … ?​
In fact none and all ! As it just depends on the original poster : the best code is the one he can understand and maintain himself !​
And as Excel features can be easily operated so with the help of the Macro Recorder it's easier for some beginners to start to learn VBA …​
And like VBA usual coders, I prefer to avoid sometimes a 'lot' of codelines by using just an Excel feature which already does well the job​
like for example the post #18 thread in link just needing two codelines :​
the first codeline must be an Excel formula allocated to a worksheet cell using COUNTIF or MATCH whatever and​
the second - last - codeline is just an advanced filter using this cell in the criteria, that's all folks !​
My post in this thread was to make the original poster think how can it be possible to do it manually and to code it just with 2 codelines​
but he did not ask for and as the 'MrExcel MVP' must know this classic Excel way but continue on his own​
so I won't waste any time following « Solution belongs to well readers … »​
Of course if I had been the first to reply I would have posted my bunch of two codelines !​
I sent it to a VBA beginner kid as a 'lockdown exercice' (no school during 3 weeks) and he well found this Excel solution​
operating manually - the 'difficult' part for him was the formula - and just using the Macro Recorder​
but he asked me : « Too easy, how can it be possible to ask for that in a forum, are you kidding me ?‼ »​
Forgive him, he's so young.​
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,052
Members
452,542
Latest member
Bricklin

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