Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

Need to lower a sum of values while keeping another sum as constant as possible

$
0
0

Ive been given a task to reduce one sum of values while keeping another some as constat as possible. I have two contributions:

Item Number, Contribution 1, Contribution 2 1, 100, 200 2, 150, 300 3, 200, -400 4, 300, 500 5, 100, -200 ...

(There are a about 5000 entries like this, with vatiable Contribution 1 and 2).

I need to remove one item (row) at the time and output the following results for each iteration: Sum of Contribution 1 Sum of Contribution 2

I did this with the following VBA code:

For i = 1 To 5000
    If .Cells(i, 2) <> 0 Then
        .Cells(i, 2) = 0
        .Cells(i, 3) = 0



        Application.Calculate

.Cells(i, 5) = Sheets("results").Cells(45, 5)
.Cells(i, 6) = Sheets("results").Cells(25, 5)
//where results sheet contains the sum of column 2 and 3
    End If

    If i Mod 500 = 0 Then
        Debug.Print i
    End If
Next i
End With

However I need to add contrains where: While get Sum of Contributions 1 as close as possible to zero Keep Sum of Contributions 2 as it was. (within 10% of original value)

Before removing an item the code needs to check if Sum of Contributions 1 will bring it closer to zero and Sum of Contributions 2 is still within 10% of original value.

Greatly appriciate your help. Can be done in VBA or Python. Please let me know if you have any questions.

Kind Regards,

Maks


Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>