Finding Max/Min/Avg of the sum of user defined no. of blocks

ABHISKV4

New Member
Joined
May 26, 2009
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Assume that i have 156 cells in a row running from cell C3 to FB3. Now I want the "block size" to be user input (call it 1) and the "no. of highest sum blocks" to be found also as an user input (call it 2). Based on these two user inputs I want to arrive at the formula or macro to find the top no. of blocks and give the Max of sum of each block, Min of sum of each block and the Average of sum of each block. Here if the user defines the block size as 10, then the no. of blocks can be maximum 15 as 10*15 is 150 which is less than 156 cells available, the moment users provides no. of blocks as 16, it needs to have minimum 160 cells. Similarly for a block size of 8 the no. of blocks can be maximum 19, 8*19 is 152.
I hope I have not made the situation too complex. Both excel formula (with or without helper cells, preferably with minimum helper cells :) and VBA code are equally welcome as a solution to this problem.

EXCEL - Finding block having the maximum sum for a range of cells ADVANCED 14Sep2020.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCCCDCECFCGCHCICJCKCLCMCNCOCPCQCRCSCTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDJDKDLDMDNDODPDQDRDSDTDUDVDWDXDYDZEAEBECEDEEEFEGEHEIEJEKELEMENEOEPEQERESETEUEVEWEXEYEZFAFB
1
2Week1Week2Week3Week4Week5Week6Week7Week8Week9Week10Week11Week12Week13Week14Week15Week16Week17Week18Week19Week20Week21Week22Week23Week24Week25Week26Week27Week28Week29Week30Week31Week32Week33Week34Week35Week36Week37Week38Week39Week40Week41Week42Week43Week44Week45Week46Week47Week48Week49Week50Week51Week52Week53Week54Week55Week56Week57Week58Week59Week60Week61Week62Week63Week64Week65Week66Week67Week68Week69Week70Week71Week72Week73Week74Week75Week76Week77Week78Week79Week80Week81Week82Week83Week84Week85Week86Week87Week88Week89Week90Week91Week92Week93Week94Week95Week96Week97Week98Week99Week100Week101Week102Week103Week104Week105Week106Week107Week108Week109Week110Week111Week112Week113Week114Week115Week116Week117Week118Week119Week120Week121Week122Week123Week124Week125Week126Week127Week128Week129Week130Week131Week132Week133Week134Week135Week136Week137Week138Week139Week140Week141Week142Week143Week144Week145Week146Week147Week148Week149Week150Week151Week152Week153Week154Week155Week156
3Sales Qty -->266345259244250156493332160375244191344220334288324375322208219344313632653151862943983413516637644190193437316327437116529063194385345350972981232121582506918319035939829130101803632842623923177617321017632614027835765143302281135781692531091792529999295241793521761515933953121329279162517138931728016335496272242107343602141873573081781784883134257231343361115112382709796122208378257134154372179381293228161346128751277832375282247
4Yellow indicates User Input, always an integer
5Green indicates, formula/answer needed in this cell
6
7User Input
81. Block Size (No. of continuous cells in a block)6
92. No. of Non Overlapping Blocks (Highest Sum block as Block#1 and likewise)4
103. Out of above defined no. of blocks (Point 2 above), sum of block holding maximum sum400
114. Out of above defined no. of blocks (Point 2 above), sum of block holding minimum sum280
125. Out of above defined no. of blocks (Point 2 above), average of the sum of each block335
13
14Illustration of expected Answers:
15Assume Block 1 Sum is400
16Assume Block 2 Sum is360
17Assume Block 3 Sum is300
18Assume Block 4 Sum is280
Sheet1
Cell Formulas
RangeFormula
D10D10=MAX(C15:C18)
D11D11=MIN(C15:C18)
D12D12=AVERAGE(C15:C18)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about:

VBA Code:
Sub Max_Min_Avg()
'Finding Max/Min/Avg of the sum of user defined no. of blocks
  Dim a As Variant
  Dim i As Long, j As Long, k As Long
  Dim nSize As Long, nBlock As Long, nTotal As Long
  Dim nSum As Long, nAcum As Long, nMax As Long, nMin As Long
  
  a = Range("C3", Cells(3, Columns.Count).End(1)).Value2
  
  nSize = Range("C8").Value     'Block Size (No. of continuous cells in a block)
  nBlock = Range("C9").Value    'No. of Non Overlapping Blocks
  nTotal = nSize * nBlock       'Total blocks
  
  nMax = 0
  nMin = 9 ^ 9
  
  If nTotal > 156 Then
    MsgBox "The number exceeds 156"
    Exit Sub
  End If
  
  For i = 1 To nTotal Step nSize
  
    nSum = 0
    For j = 1 To nSize
      k = k + 1
      nSum = nSum + a(1, k)
      nAcum = nAcum + a(1, k)
    Next
    
    If nSum < nMin Then nMin = nSum
    If nSum > nMax Then nMax = nSum
    
  Next
  
  Range("C10").Value = nMax
  Range("C11").Value = nMin
  Range("C12").Value = nAcum / nBlock

End Sub
 
Upvote 0
How about:
Many thanks for your code. My apologies If i didn't make it clear enough in the first post. Blocks are all types of combinations of 6 continuous cells but then when we pick the top no. of blocks they cannot be overlapping with each other. So Block1 is C3:H3, Block2 is D3:I3, Block3 is E3:J3 and so on. With above logic, the block of 6 cells with highest sum would be 1885, minimum as 1674 and average as 1789 (i have found the blocks manually one by one). May be the excel snapshot, see below, be of help in understanding the problem better:
EXCEL - Finding block having the maximum sum for a range of cells ADVANCED 14Sep2020.xlsx
CDEFGH
1Block#
2Block11885Max
3Block21799
4Block31798
5Block41674min
61789Average
7
8Week#Sales UnitsRunning Total of 6 cellsBlock#Remarks
9Week12661520
10Week23451303
11Week32591291
12Week42441053
13Week5250869
14Week6156994
15Week7491082
16Week83331224
17Week9211235Can't be used as overlaps with Block1
18Week10601434Can't be used as overlaps with Block1
19Week113751708Can't be used as overlaps with Block1
20Week122441621Can't be used as overlaps with Block1
21Week131911701Can't be used as overlaps with Block1
22Week143441885Block1Can't be used as overlaps with Block1
23Week152201863Block1Can't be used as overlaps with Block1
24Week163341851Block1Can't be used as overlaps with Block1
25Week172881736Block1Can't be used as overlaps with Block1
26Week183241792Block1Can't be used as overlaps with Block1
27Week193751781Block1Can't be used as overlaps with Block1
28Week203221469
29Week212081412Can't be used as overlaps with Block2
30Week222191519Can't be used as overlaps with Block2
31Week233441486Can't be used as overlaps with Block2
32Week243131436Can't be used as overlaps with Block2
33Week25631521Can't be used as overlaps with Block2
34Week262651799Block2Can't be used as overlaps with Block2
35Week273151569Block2Can't be used as overlaps with Block2
36Week281861420Block2Can't be used as overlaps with Block2
37Week292941610Block2Can't be used as overlaps with Block2
38Week303981360Block2Can't be used as overlaps with Block2
39Week313411152Block2Can't be used as overlaps with Block2
40Week3235830
41Week33166829
42Week343761036
43Week3544823
44Week361901053
45Week37191234
46Week38341380
47Week393731636
48Week401631326
49Week412741357
50Week423711468
51Week431651442
52Week442901627
53Week45631434
54Week461941669
55Week473851598
56Week483451425
57Week493501238
58Week50971138
59Week512981110
60Week52123995
61Week532121062
62Week541581209
63Week552501449
64Week56691490
65Week571831451
66Week581901278
67Week593591268Can't be used as overlaps with Block3
68Week603981272Can't be used as overlaps with Block3
69Week612911158Can't be used as overlaps with Block3
70Week62301129Can't be used as overlaps with Block3
71Week63101491Can't be used as overlaps with Block3
72Week641801798Block3Can't be used as overlaps with Block3
73Week653631694Block3Can't be used as overlaps with Block3
74Week662841504Block3Can't be used as overlaps with Block3
75Week672621430Block3Can't be used as overlaps with Block3
76Week683921344Block3Can't be used as overlaps with Block3
77Week693171278Block3Can't be used as overlaps with Block3
78Week70761101
79Week711731303
80Week722101487
81Week731761342
82Week743261309
83Week751401285
84Week762781426
85Week773571283
86Week78651004
87Week791431108
88Week803021218
89Week812811025
90Week82135923
91Week8378813
92Week841691034
93Week85253964
94Week861091006
95Week87179921
96Week8825921
97Week892991248
98Week90991125
99Week912951177
100Week9224941
101Week931791256
102Week943521130
103Week95176899
104Week961511052
105Week97591180
106Week983391137
107Week9953823
108Week100121941Can't be used as overlaps with Block4
109Week1013291209Can't be used as overlaps with Block4
110Week1022791197Can't be used as overlaps with Block4
111Week103161198Can't be used as overlaps with Block4
112Week104251345Can't be used as overlaps with Block4
113Week1051711674Block4Can't be used as overlaps with Block4
114Week1063891599Block4Can't be used as overlaps with Block4
115Week1073171482Block4Can't be used as overlaps with Block4
116Week1082801407Block4Can't be used as overlaps with Block4
117Week1091631234Block4Can't be used as overlaps with Block4
118Week1103541105Block4Can't be used as overlaps with Block4
119Week111961111
120Week1122721229
121Week1132421144
122Week1141071259
123Week115341460
124Week1163601604
125Week1172141422
126Week1181871256
127Week1193571152
128Week120308929
129Week121178878
130Week122178931
131Week123481096
132Week124831409
133Week1251341441
134Week1262571318
135Week1272311299
136Week1283431338
137Week1293611092
138Week130115827
139Week13111834
140Week1322381031
141Week1332701171
142Week134971158
143Week135961195
144Week1361221253
145Week1372081503
146Week1383781474
147Week1392571477
148Week1401341513
149Week1411541607
150Week1423721614
151Week1431791588
152Week1443811537
153Week1452931231
154Week1462281065
155Week147161915
156Week148346786
157Week149128815
158Week15075969
159Week1511271141
160Week15278
161Week15332
162Week154375
163Week155282
164Week156247
Sheet2
Cell Formulas
RangeFormula
G2:G5G2=SUMIF($F$9:$F$164,F2,$D$9:$D$164)
G6G6=AVERAGE(G2:G5)
E9:E159E9=SUM(D9:D14)
 
Upvote 0
So Block1 is C3:H3, Block2 is D3:I3, Block3 is E3:J3 and so on. With above logic, the block of 6 cells with highest sum would be 1885, minimum as 1674 and average as 1789

Now I understand less. In your first example the structure is horizontal now it is vertical, that doesn't help. The macro is not dynamic to adapt to changes in the structure of your data.

So why in your example the first block is C3: H3, but in your data range the first block is not the first cell, the first block according to the yellow color is up to cell D22, this is very confusing for me.

It looks like you get all 4 blocks at random.
 
Upvote 0
Now I understand less. In your first example the structure is horizontal now it is vertical, that doesn't help. The macro is not dynamic to adapt to changes in the structure of your data.
My apologies if it is confusing. Please note that I transposed the data from horizontal to vertical just for my ease of calculations and finding the desired blocks. The data remains horizontally only as posted in the original post. First block still remains C3:H3, 2nd block D3:I3, and so on.
So in 2nd excel snapshot (the transposed version), the equivalent first block becomes D9:D14, 2nd block D10:D15, and so on. The range highlighted in yellow (D22:D27) is the block of 6 cells which is having the highest sum, I did the calculations manually and figured out the maximum sum block just for conveying the expected solution to the original problem. Once Block1 is decided, any sum range of continuous 6 cells cannot have even a single cell which is already used by Block1, and that's how we find the 2nd highest block, and so on. Please see the excel snapshot again and may be you will understand what I am looking for. Happy to further elaborate if still not clear, thanks.
 
Upvote 0
Start with this (with the sales data going off to the right as far as needed):

Book1
ABCDE
1
2Week1Week2Week3
3Sales Qty -->266345259
4
5
6
7User Input
81. Block Size (No. of continuous cells in a block)6
92. No. of Non Overlapping Blocks (Highest Sum block as Block#1 and likewise)4
Sheet1


Try this macro:

VBA Code:
Sub BlockMax()
Dim SalesData As Variant, BlkSize As Long, NumBlks As Long
Dim Weeks(), MaxWeek As Long, MaxSum As Double, BlockNo As Long
Dim i As Long, j As Long, sd As Range, tlc As Range

    Set sd = Range("C3")            ' Make this the leftmost cell of the sales data
    BlkSize = Range("C8").Value     ' This is where you put the block size
    NumBlks = Range("C9").Value     ' This is where you put the number of blocks you want
    Set tlc = Range("C10")          ' This is the top left corner of where you want the results
    
    SalesData = Range(sd, Cells(sd.Row, Cells(sd.Row, Columns.Count).End(xlToLeft).Column)).Value
    ReDim Weeks(1 To NumBlks, 1 To 2)
    BlockNo = 0
    
Loop1:
    MaxWeek = -1
    MaxSum = -1
    For i = 1 To UBound(SalesData, 2) - BlkSize + 1
        For j = 1 To BlockNo
            If i > Weeks(j, 1) - BlkSize + 1 And i < Weeks(j, 1) + BlkSize - 1 Then GoTo IterateI:
        Next j
        wksum = 0
        For j = i To i + BlkSize - 1
            wksum = wksum + SalesData(1, j)
        Next j
        If wksum > MaxSum Then
            MaxSum = wksum
            MaxWeek = i
        End If
IterateI:
    Next i
    
    If MaxWeek = -1 Then
        MsgBox "Unable to come up with enough non-overlapping blocks."
        Exit Sub
    End If
    
    BlockNo = BlockNo + 1
    Weeks(BlockNo, 1) = MaxWeek
    Weeks(BlockNo, 2) = MaxSum
    
    If BlockNo < NumBlks Then GoTo Loop1:
    
    tlc.Resize(, 2).Value = Array("Week starting", "Sum")
    tlc.Offset(1).Resize(NumBlks, 2).Value = Weeks
    
    tlc.Offset(NumBlks + 1) = "Average"
    tlc.Offset(NumBlks + 1, 1).FormulaR1C1 = "=AVERAGE(R[-" & NumBlks & "]C:R[-1]C)"
    
End Sub

Note the first 4 lines at the top of the macro where you can set the locations of the input and output cells.

This is the result:

Book1
ABCDE
1
2Week1Week2Week3
3Sales Qty -->266345259
4
5
6
7User Input
81. Block Size (No. of continuous cells in a block)6
92. No. of Non Overlapping Blocks (Highest Sum block as Block#1 and likewise)4
10Week startingSum
11141885
12261799
13641798
14191781
15Average1815.75
Sheet1
Cell Formulas
RangeFormula
D15D15=AVERAGE(D11:D14)
 
Upvote 0
This is the result:
This is exactly what I was looking for, thank you so much for this awesome code. Please note that the macro output gives Week starting as 14,16,64 and 19. Cell U3 (highlighted in Red), which is the last week of the first block (which starts from week 14) overlaps with the first week of last block (which starts from Week19). If you could fix this please, then it's perfect.
Book3
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1
2Week1Week2Week3Week4Week5Week6Week7Week8Week9Week10Week11Week12Week13Week14Week15Week16Week17Week18Week19Week20Week21Week22Week23Week24Week25Week26Week27
326634525924425015649333216037524419134422033428832437532220821934431363265315
4Block1Block1Block1Block1Block1Block1
5Block4Block4Block4Block4Block4Block4
Sheet1
 
Upvote 0
Dagnabbit! ? Pesky little signs, OK, change this line:

Rich (BB code):
If i > Weeks(j, 1) - BlkSize + 1 And i < Weeks(j, 1) + BlkSize - 1 Then GoTo IterateI:

to

Rich (BB code):
If i >= Weeks(j, 1) - BlkSize + 1 And i <= Weeks(j, 1) + BlkSize - 1 Then GoTo IterateI:
 
Upvote 0
If i >= Weeks(j, 1) - BlkSize + 1 And i <= Weeks(j, 1) + BlkSize - 1 Then GoTo IterateI:
Wow, it works just the way I wanted it to:). Many many thanks for this great help. I never thought this would be really possible in excel, but you have proved me wrong, and slowly I realize how powerful the VBA is. Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,496
Members
449,089
Latest member
Raviguru

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