I didn’t really want to come back to this one. But since it’s continued on with some interesting views expressed, I guess there’s some interest in where it leads.
(a) Jindon’s code. I had hoped he’d be around to explain this himself. His approach was to fill (each cell of) an excel range with a random number formula, then convert the formula to values, then to put the values into a range/array (called a) in which he then counted the < =1 using a neat and simple bit of code.
I restate that approach below, and hope my reformulation answers the questions you asked about his meanings.
Also I give the data generation, the Jindon approach, and the CountIf approach recommended by Nate all together, with each part timed by the Excel timer.
You might play around around with the size of the date set to count the <=1 (by changing rws and cls as you like near the top). Currently it’s 2 million random numbers.
I think you’ll find Jindon’s counting method gives the same (correct) result faster for ALL data sets that are of a largish size, right up to the limits of you computer’s RAM, than does the CountIf approach.
Now, why do you think this is so? You might also assess the speed of Nate’s code, for comparison purposes.
CountIf is a worksheet function, suited to work on worksheets and best used there. Array-based approaches are better suited to VBA. You can there handle problems almost as complex as you like, and often, as your current problem shows, handle even the simpler ones faster than can worksheet functions.
Code:
Sub allup()
[a:iv].ClearContents
td = Timer
rws = 50000: cls = 40
Set a = Range([a1], Cells(rws, cls))
a.Formula = "=rand()*2"
a = a.Value
MsgBox "Range/array generated and listed in " & Timer - td & " secs"
'VBA counting from array
tm = Timer
For Each e In a
If e <= 1 Then p = p + 1
Next
x = Timer - tm
MsgBox "VBA counted " & p & " <=1 in " & x & " secs"
'CountIf counting from range on spreadsheet
tf = Timer
q = WorksheetFunction.CountIf([a1].CurrentRegion, "<=1")
y = Timer - tf
MsgBox "CountIf counted " & q & " <=1 in " & y & " secs"
If x > 0 And y > 0 Then _
MsgBox "Formula (CountIf) approach is " _
& Format((y / x - 1) * 100, "0.0") & " percent slower"
End Sub
(b) Nate’s comments are generally good, but his timing method is not. Using “now” to express his current time his time measurements are in days. Not very surprising then that the time taken over this sort of problem barely registers.
The excel timer uses units of 1/64 seconds and gives a much better idea of how long a procedure actually takes.
(c) Arrays and ranges. An array is an ordered collection of data. A range (in excel) is a specified cell or number of cells, which needn’t even contain any data. Excel ranges have at most two dimensions, whereas arrays can have as many as you like, and it’s sometimes useful to use plenty. Most arrays can’t be shown on spreadsheets. and confining oneself to only the ones that can is pretty restrictive.
I used both range and array in my initial post above. That was to find just what you wanted as much as to provide a solution.
(d) Dimensioning or declaring in Excel VBA. This is a in a interesting and debatable topic. Not to say its never necessary, but in most cases appearing on this forum the suggested codes are overdimmed by very large amounts. Keep your codes as straightforward and uncluttered as possible and you’ll have less problems with them, just as you might in other aspects of your life.
Long codes and complex problems are a different issue, and keeping close track of all variables in then a good idea.
I note you tried dimensioning Jindon’s code. Entirely unnecessary complexity in that case and a waste of time and space. If you don’t dim that code it runs perfectly (or does on my machine). If you had problems running it, dimming or not dimming was NOT the problem.
There’s a long established philosophical principle, called Ockam’s Razor, which goes back some 7 centuries to a group of thinkers called the Medieval Scholastics (real interesting bunch, Duns Scotus, Peter Abelard who got castrated because of his disapproved love affair with Heloise, Roger Bacon who invented gunpowder, Desiderius Erasmus, Thomas Acquinas etc. This isn’t the right forum for this sort of stuff and maybe not for me either.) One version is “it is vain to do with more what can be done with fewer”, another is “entities should not be multiplied beyond necessity”. This idea has had significant influence on for example the development of evolutionary theory on biology.
Personally, I also like it when applied to code-writing, and only dim when I see it as useful, which is not that often. Never had a problem that I recall, and I’ve done quite a lot of code-writing.
Of course must one respect Nate’s view that the way he does it is “doing it right”. I guess most of us think that way.