Simple Histogram in VBA?

牧云@^-^@ 提交于 2021-02-10 18:36:18

问题


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).

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.

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.)

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.

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) <= breaks(1)) Then freq(1) = freq(1) + 1
    If (arr(i) >= breaks(M - 1)) Then freq(M) = freq(M) + 1
    For j = 2 To M - 1
        If (arr(i) > breaks(j - 1) And arr(i) <= 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

And then I try to call it simply by:

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

回答1:


Not 100% sure as to the efficacy of that approach but;

  • Remove the parens as your calling a sub; Hist M, arr
  • M is declared as double but received by the function as a long; this won't work so declare it in the calling routine as long
  • You will need to recieve arr() As Variant
  • Range -> Array produces a 2 dimensional array so the elements are arr(1, 1) .. arr(n, 1)



回答2:


A little late but still I want to share my solution. I created a Histogram function which might be used as array formula in the excel spread sheet. Note: you must press CTRL+SHIFT+ENTER to enter the formula into your workbook. Input is the range of values and the number M of bins for the histogram. The output range must have M rows and two columns. One column for the bin value and one column for the bin frequency.

Option Explicit
Option Base 1

Public Function Histogram(arr As Range, M As Long) As Variant
On Error GoTo ErrHandler
    Dim val() As Variant
    val = arr.Value
    Dim i As Long, j As Integer
    Dim Length As Single
    ReDim breaks(M) As Single
    ReDim freq(M) As Integer

    Dim min As Single
    min = WorksheetFunction.min(val)
    Dim max As Single
    max = WorksheetFunction.max(val)

    Length = (max - min) / M

    For i = 1 To M
        breaks(i) = min + Length * i
        freq(i) = 0
    Next i

    For i = 1 To UBound(val)
        If IsNumeric(val(i, 1)) And Not IsEmpty(val(i, 1)) Then
            If val(i, 1) > breaks(M) Then
                freq(M) = freq(M) + 1
            Else
                j = Int((val(i, 1) - min) / Length) + 1
                freq(j) = freq(j) + 1
            End If
        End If
    Next i

    Dim res() As Variant
    ReDim res(M, 2)
    For i = 1 To M
        res(i, 1) = breaks(i)
        res(i, 2) = freq(i)
    Next i

    Histogram = res
ErrHandler:
    'Debug.Print Err.Description
End Function


来源:https://stackoverflow.com/questions/9004409/simple-histogram-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!