Maximum Total Optimization - VBA - Need Help

canernas

New Member
Joined
Mar 18, 2025
Messages
2
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Dear MrExcel Family,

I am working on a project. The goal of this project is to minimize the SUM formulas in the column totals located in row 14 of the table found in the attached Excel file. Rows 3 to 12 specify the total demand for each 5-minute interval, cell by cell. To minimize the column totals in row 14, the numbers in the rows need to be shifted. By shifting these numbers, I will obtain the most optimal subtotals. There are some constraints while performing this operation:
  1. The numbers to be shifted can only be moved as groups. (For example, a group of numbers written as 3 - 3 - 3 in one row must remain 3 - 3 - 3 when moved.)
  2. The shifts should only occur between the blue cells and should not go outside the blue cells.
  3. The number group in one row cannot be moved to a different row; the shifting must occur within the same row.
I have been trying to implement this optimization using VBA code for days, but I couldn't find a solution. I would appreciate your support, as I really haven't been able to find a way out! :)
 

Attachments

  • Screenshot 2025-03-18 205554.png
    Screenshot 2025-03-18 205554.png
    18.9 KB · Views: 12
Welcome to the MrExcel Message Board! :)

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Welcome to the Forum!

The goal of this project is to minimize the SUM formulas ...
Not sure what you mean here, as the total sum won't vary if you slide the row values left or right.

I suspect you want column totals to be as uniform as possible, i.e. to solve for minimum column total variance, or minimum difference between min and max column totals?

What exactly is a group? Does each row have one group, so that in your row 9, for example, there are only four possibilities:

22211
22211
22211
22211
 
Upvote 0
Welcome to the Forum!


Not sure what you mean here, as the total sum won't vary if you slide the row values left or right.

I suspect you want column totals to be as uniform as possible, i.e. to solve for minimum column total variance, or minimum difference between min and max column totals?

What exactly is a group? Does each row have one group, so that in your row 9, for example, there are only four possibilities:

22211
22211
22211
22211


Hello Stephen,

Firstly, thank you for your response.

Yes I am trying to reduce the column totals as much as I can to optimize the solution. But the main focus is to have the minimum of column totals because everytime we move a number between blue cells, some columns totals will change. By chaning number positions, we will be trying to find the minimum number.

By group I mean the number of cells together in a single row. For example, as your screenshot, the 2-2-2-1-1 is one number group. and we can only move this number of group between the blue cells.

22211


I hope I was able to answer your questions .


Thanks again
 
Upvote 0
By chaning number positions, we will be trying to find the minimum number ....
I think you're saying you don't want large numbers in any column, as this will mean small numbers in other columns, and hence too much variability in the column totals.

Try this:

ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAU
1GrpValsLenMinMaxSelect12345678910111213141516171819202122232425262728
21333335111133333
3222237108 222
4322224182518 2222
54333335111811 33333
65333311176117 3333111
763333352123 33333
87222115121212 22211
98112242727 11
109222115212422 22211
111033333333333331371616 3333333333333
12
13Total3366636555466544355555554444
14
15Solve13
16Solve20.97
Sheet1
Cell Formulas
RangeFormula
A2:A11A2=SEQUENCE(ROWS(B2:B11))
O2:O11O2=COUNT(B2:N2)
T1:AU1T1=SEQUENCE(,28)
T2:AU11T2=LET(n,T$1#-R2+1,IF((n>0)*(n<=O2),INDEX(B2:N2,n),""))
T13:AU13T13=SUM(T2:T11)
T15T15=MAX(T13:AU13)-MIN(T13:AU13)
T16T16=VAR(T13:AU13)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
solver_adj=Sheet1!$R$2:$R$11T2
solver_lhs1=Sheet1!$R$2T2
solver_lhs10=Sheet1!$R$5T5
solver_lhs11=Sheet1!$R$5T5
solver_lhs12=Sheet1!$R$5T5
solver_lhs13=Sheet1!$R$6T6
solver_lhs14=Sheet1!$R$6T6
solver_lhs15=Sheet1!$R$6T6
solver_lhs16=Sheet1!$R$7T7
solver_lhs17=Sheet1!$R$7T7
solver_lhs18=Sheet1!$R$7T7
solver_lhs19=Sheet1!$R$8T8
solver_lhs2=Sheet1!$R$2T2
solver_lhs20=Sheet1!$R$8T8
solver_lhs21=Sheet1!$R$8T8
solver_lhs22=Sheet1!$R$9T9
solver_lhs23=Sheet1!$R$9T9
solver_lhs24=Sheet1!$R$9T9
solver_lhs25=Sheet1!$R$10T10
solver_lhs26=Sheet1!$R$10T10
solver_lhs27=Sheet1!$R$10T10
solver_lhs28=Sheet1!$R$11T11
solver_lhs29=Sheet1!$R$11T11
solver_lhs3=Sheet1!$R$2T2
solver_lhs30=Sheet1!$R$11T11
solver_lhs4=Sheet1!$R$3T3
solver_lhs5=Sheet1!$R$3T3
solver_lhs6=Sheet1!$R$3T3
solver_lhs7=Sheet1!$R$4T4
solver_lhs8=Sheet1!$R$4T4
solver_lhs9=Sheet1!$R$4T4


I used code to set up Solver more quickly, i.e. rather than adding 30 constraints manually.

VBA Code:
Sub Optimise()

    Dim i As Long, RunTime As Long, SolveOption As Long
   
    SolveOption = 2
    RunTime = 10 'say
   
    SolverReset
    SolverOk SetCell:=IIf(SolveOption = 1, "T15", "T16"), MaxMinVal:=2, ValueOf:=0, ByChange:="R2:R11", Engine:=3, EngineDesc:="Evolutionary"
    SolverOptions MaxTime:=0, Iterations:=0, Precision:=0.000001, Convergence:=0.0001, StepThru:=False, Scaling:=True, AssumeNonNeg:=True, Derivatives:=1
    SolverOptions PopulationSize:=100, RandomSeed:=0, MutationRate:=0.075, Multistart _
        :=False, RequireBounds:=True, MaxSubproblems:=0, MaxIntegerSols:=0, _
        IntTolerance:=1, SolveWithout:=False, MaxTimeNoImp:=RunTime
       
    For i = 2 To 11
        SolverAdd CellRef:="R" & i, Relation:=4, FormulaText:="integer"
        SolverAdd CellRef:="R" & i, Relation:=1, FormulaText:="Q" & i
        SolverAdd CellRef:="R" & i, Relation:=3, FormulaText:="P" & i
    Next i
       
    SolverSolve

End Sub

The results in R2:R11 are one of several similar solutions when SolveOption = 2, i.e. minimising the variance of the column totals.
 
Last edited:
Upvote 0

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