title: Simple Histogram in VBA?
body: <p>I have data stored in some column (Say, Column A).  The length of Column A is not fixed (depends on previous steps in the code).  </p>
  
  <p>I need a histogram for the values in Column A, and have it in the same sheet.  I need to take the values in column A, and automatically compute M Bins, then give the plot.</p>
  
  <p>I looked online for a "simple" code, but all codes are really fancy, with tons of details that I don't need, to the extent that I am not even able to use it. (I am a VBA beginner.)</p>
  
  <p>I found the following code that seems to do the job, but I am having trouble even calling the function.  Besides, it only does computations but does not make the plot.</p>
  
  <pre><code>Sub Hist(M As Long, arr() As Single)
  Dim i As Long, j As Long
  Dim Length As Single
  ReDim breaks(M) As Single
  ReDim freq(M) As Single
  
  For i = 1 To M
      freq(i) = 0
  Next i
  
  Length = (arr(UBound(arr)) - arr(1)) / M
  
  For i = 1 To M
      breaks(i) = arr(1) + Length * i
  Next i
  
  For i = 1 To UBound(arr)
      If (arr(i) &lt;= breaks(1)) Then freq(1) = freq(1) + 1
      If (arr(i) &gt;= breaks(M - 1)) Then freq(M) = freq(M) + 1
      For j = 2 To M - 1
          If (arr(i) &gt; breaks(j - 1) And arr(i) &lt;= breaks(j)) Then freq(j) = freq(j) + 1
      Next j
  Next i
  
  For i = 1 To M
      Cells(i, 1) = breaks(i)
      Cells(i, 2) = freq(i)
  Next i
  End Sub
  </code></pre>
  
  <p>And then I try to call it simply by:</p>
  
  <pre><code>Sub TestTrial()
  Dim arr() As Variant
  Dim M As Double
  Dim N As Range
  
  arr = Range("A1:A10").Value
  M = 10
  
  Hist(M, arr)    ' This does not work.  Gives me Error (= Expected)
  End Sub
  </code></pre>