Macro to average groups of values that fall within "X" points of each other

kvcornie

New Member
Joined
Oct 12, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a list of numerical values in a single column that are sorted from largest to smallest.

I need a macro that will loop through this list looking for "groups" of values that are within "x" points of each other AND whose total range is less than "Y" points or a "MaxRange".

The screenshot below is an example of a list and highlights the groups of values that meet both of these criteria. i.e. In the first group 74 is < 3 points from 72 and 72 is < 3 points from 70, AND the total range of 74-70 < 6 points.

The "Output Range" (C6-C8) is my desired result from the macro.

Thank you for any help!

1634050689892.png
 
After using this code, I found an issue where it appeared the MaxRange was being used to evaluate first and then digging further I found that the "XValue" was not being used in the code, only the variable was declared. I tried to fix it by replacing MaxRange w/ XValue in the code, and it is now pulling some of the correct groups of data that are within the XValue from the next value in the list, but it is also missing some of the groups and of course I don't have MaxRange incorporated.
I guess that's on me. I read your initial requirements and didn't quite grasp the implications. But it seems clear enough now that you've explained it again. Try this version of the macro:

VBA Code:
Sub FindGroups()
Dim MyDataLoc As Range, XValue As Double, MaxRange As Double, OutRange As Range
Dim MyData As Variant, ar() As Variant, ctr As Long, i As Long, j As Long, TempSum As Double

    Set MyDataLoc = Range("K5")
    XValue = Range("K2").Value
    MaxRange = Range("K3").Value
    Set OutRange = Range("M5")
    
    MyData = Range(MyDataLoc, MyDataLoc.End(xlDown)).Value
    ReDim ar(1 To UBound(MyData), 1 To 2)
    OutRange.Resize(UBound(MyData), 2).ClearContents
    ctr = 0
    
    For i = 1 To UBound(MyData)
        TempSum = MyData(i, 1)
        For j = i + 1 To UBound(MyData)
            If MyData(j, 1) < MyData(i, 1) - MaxRange Then Exit For
            If MyData(j - 1, 1) - MyData(j, 1) > XValue Then Exit For
            TempSum = TempSum + MyData(j, 1)
        Next j
        j = j - 1
        If j > i Then
           ctr = ctr + 1
           ar(ctr, 1) = TempSum / (j - i + 1)
           ar(ctr, 2) = "Group " & ctr & " (" & MyData(i, 1) & "-" & MyData(j, 1) & ")"
           i = j + 1
        End If
    Next i
    
    OutRange.Resize(ctr, 2) = ar
End Sub

I made a few other tweaks, like using JEC's suggestion for outputting the results, and clearing the output area, but the main change was adding the extra IF using the XValue. I checked the macro using your sample, and it looks like it's doing what you want, but you need to check it too. When I ran the original macro on that sample, I did not get your results, which I can't explain.

It looks like I'm not able to upload the actual spreadsheet,

There is a tool on this forum called XL2BB. See the link in my signature or the reply box. It's easy to download, install, and use. Here's an example:

ME temp.xlsm
JKLMN
1
2ClustPoints_02 -->0.099
3ClustMaxRange_02 -->0.297
4
5ClustValueStart_02 -->212.8883.60667Group 1 (83.65-83.57)
6178.1882.895Group 2 (82.94-82.85)
7143.4882.115Group 3 (82.2-82.04)
8114.1581.53Group 4 (81.56-81.5)
9108.7981.005Group 5 (81.01-81)
10104.6480.39Group 6 (80.39-80.39)
1199.8
1295.71
1395.13
1493.88
1591
1690.24
1789.16
1888.21
1987.96
2087.32
2186.18
2285.63
2385.49
2484.6
2584.45
2684.14
2783.65
2883.6
2983.57
3083.18
3182.94
3282.85
3382.62
3482.5
3582.4
3682.2
3782.11
3882.11
3982.04
4081.58
4181.56
4281.5
4381.01
4481.01
4581
4680.9
4780.78
4880.39
4980.39
5080.05
Sheet37


Using this allows the helpers here to copy and paste your data, instead of requiring us to manually retype it. That might be why my results didn't match yours, I could have entered something wrong.

In any event, let me know how this works!
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top