joejackson123
New Member
- Joined
- Oct 27, 2017
- Messages
- 16
I'm trying to create an Excel VBA function that cycles through a <code>cycle_range</code> and calculates the correlation each range of 5 cell entries within that range to a <code>base_range</code> of 5 cell entries. The function should return the maximum correlation in the cycle_range. For example, the following data should return the 0.506..... output, as the second set of cycle range numbers, <code>7, 8, 9, 0, 8</code>, have that correlation to the base range, which is the highest correlation:
<code>
</code>
The code I have thus far is below, it doesn't work. There is very clearly a problem with adding rng and elements together from cycle_range, but not sure what to do:
As always, any advice is very much appreciated, I'm having a tough time with this one. Thank you!
P.S. I cribbed some stuff from here - vba pass a group of cells as range to function
<code>
</code>
Code:
<code>cycle range base range output
4 3 0.506253796
7 7
8 3
9 2
0 9
8
5
4 </code>
The code I have thus far is below, it doesn't work. There is very clearly a problem with adding rng and elements together from cycle_range, but not sure what to do:
Code:
<code>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</code>
P.S. I cribbed some stuff from here - vba pass a group of cells as range to function
Last edited: