VBA 中的简单直方图?
我将数据存储在某个列中(例如,A 列)。 A 列的长度不固定(取决于代码中之前的步骤)。
我需要 A 列中的值的直方图,并将其放在同一张表中。我需要获取 A 列中的值,并自动计算 M 个 Bin,然后给出绘图。
我在网上寻找了一个“简单”的代码,但所有代码都非常花哨,有大量我不需要的细节,以至于我什至无法使用它。 (我是 VBA 初学者。)
我发现以下代码似乎可以完成这项工作,但我什至在调用该函数时都遇到了麻烦。此外,它只进行计算,但不进行绘图。
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
然后我尝试简单地调用它:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有点晚了,但我仍然想分享我的解决方案。我创建了一个
Histogram
函数,可以用作 Excel 电子表格中的数组公式。注意:必须按CTRL+SHIFT+ENTER
将公式输入到工作簿中。输入是值的范围和直方图的箱数 M。输出范围必须有 M 行和两列。一列表示 bin 值,一列表示 bin 频率。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 pressCTRL+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.不能 100% 确定该方法的有效性,但是;
Hist M, arr
M
被声明为double
,但由函数接收为long
;这不起作用,因此在调用例程中将其声明为long
arr() As Variant
Range -> Array
生成一个二维数组,因此元素为arr(1, 1) .. arr(n, 1)
Not 100% sure as to the efficacy of that approach but;
Hist M, arr
M
is declared asdouble
but received by the function as along
; this won't work so declare it in the calling routine aslong
arr() As Variant
Range -> Array
produces a 2 dimensional array so the elements arearr(1, 1) .. arr(n, 1)