VBA 中的简单直方图?

发布于 2024-12-28 19:15:01 字数 1149 浏览 3 评论 0原文

我将数据存储在某个列中(例如,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2025-01-04 19:15:01

有点晚了,但我仍然想分享我的解决方案。我创建了一个 Histogram 函数,可以用作 Excel 电子表格中的数组公式。注意:必须按
CTRL+SHIFT+ENTER 将公式输入到工作簿中。输入是值的范围和直方图的箱数 M。输出范围必须有 M 行和两列。一列表示 bin 值,一列表示 bin 频率。

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

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
〃安静 2025-01-04 19:15:01

不能 100% 确定该方法的有效性,但是;

  • 在调用 sub 时删除括号; 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;

  • 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文