【问题标题】:Excel VBA Function to cycle through range and measure multiple correlationsExcel VBA 函数循环遍历范围并测量多个相关性
【发布时间】:2018-09-29 20:57:12
【问题描述】:

我正在尝试创建一个 Excel VBA 函数,该函数循环遍历 cycle_range 并计算该范围内 5 个单元格条目的每个范围与 5 个单元格条目的 base_range 的相关性。该函数应返回 cycle_range 中的最大相关性。例如,以下数据应返回 0.506..... 输出,因为第二组循环范围数字 7, 8, 9, 0, 8 与基本范围具有相关性,这是最高相关性:

cycle range     base range       output
     4              3          0.506253796
     7              7   
     8              3   
     9              2   
     0              9   
     8      
     5      
     4  

到目前为止我的代码如下,它不起作用。从 cycle_range 中将 rng 和元素添加在一起很明显存在问题,但不知道该怎么做:

Function best_correl(correl_length As Double, base_range As Range, cycle_range As Range)


Dim i As Double
Dim rng As Range
Dim cycle_range_length As Double
Dim element As Variant
Dim max_correl As Double
Dim curr_correl As Double


cycle_range_length = cycle_range.Count - correl_length


For i = 1 To cycle_range_length

    For element = 1 To correl_length
        rng = rng + element
    Next element

    curr_correl = WorksheetFunction.Correl(base_range, rng)

    If curr_correl > max_correl Then
        max_correl = curr_correl
    End If

Next i


best_correl = max_correl


End Function

与往常一样,非常感谢任何建议,我很难接受这个建议。谢谢!

附:我从这里抄了一些东西 - vba pass a group of cells as range to function

【问题讨论】:

    标签: excel vba user-defined-functions


    【解决方案1】:
    Function best_correl(correl_length As Double, base_range As Range, cycle_range As Range)
    
        Dim c As Range
        Dim cycle_range_length As Long
        Dim max_correl As Double
        Dim curr_correl As Double, n As Long, pos as Long
    
        cycle_range_length = 1 + (cycle_range.Count - correl_length)
        n = 0
        pos = 0
        For Each c In cycle_range.Resize(cycle_range_length, 1).Cells
            n = n + 1             
            curr_correl = WorksheetFunction.Correl(base_range, c.Resize(correl_length, 1))
            If curr_correl > max_correl Then  
                max_correl = curr_correl
                pos = n  
            End If
        Next c
    
        'best_correl = max_correl '<< max correlation
        best_correl = pos         '<< position of max 
    
    End Function
    

    【讨论】:

    • @ Tim Williams 另一个问题 - 如果我想返回 max_correl 的位置而不是相关性(即如果它是循环中的第二个范围,那么 best_correl = 2),是吗是否可以让代码返回该输出?
    • 太棒了。再次感谢!
    【解决方案2】:

    我想这就是你要找的东西

    Function best_correl(correl_length As Double, base_range As Range, cycle_range As Range)
    
    Dim MyCell As Range
    Dim max_correl As Double
    Dim curr_correl As Double
    
    For Each MyCell In cycle_range
        curr_correl = Application.WorksheetFunction.Correl(base_range, MyCell)
            If curr_correl > max_correl Then
                max_correl = curr_correl
            End If
    Next MyCell
    
    best_correl = max_correl
    
    
    End Function
    

    【讨论】:

    • 谢谢,但我相信这只是逐个单元格地查看 - correl 函数必须超过一个范围。在我的示例中,循环应该有 4 次迭代,因为循环范围内总共有 9 个单元格 - 所以 5 个单元格的四个范围。可能不明白你的代码在做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多