VBA To Calculate Running Total and Output Calculation

xcellnthefuture

New Member
Joined
Nov 17, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello! I am trying to do a "Do While" loop to calculate a running total based on criteria. For the below:
1605626905519.png


What I am trying to do is while the SKU column is the same, add up all the pink quantities together, add up all the purple quantities together (make the purple quantities positive) and then output the lower number between the two on the last line of the purple for that SKU.

Here is what I did manually, that I need the VBA to do.
1605626920335.png


Here is my code that I am currently working on.

1605627802119.png
 

Attachments

  • 1605627694562.png
    1605627694562.png
    19.7 KB · Views: 11

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
try this on a copy of your file.

change your color index numbers to what you have for pink and purple

VBA Code:
Sub do_it()

'green 48
'blue 33

pinkcount = 0
purplecount = 0

For r = 2 To Cells(Rows.Count, "C").End(xlUp).Row

If Cells(r, "H").Interior.ColorIndex = 48 Then pinkcount = pinkcount + Cells(r, "H").Value
If Cells(r, "I").Interior.ColorIndex = 33 Then purplecount = purplecount + Cells(r, "I").Value

If Cells(r, "C") <> Cells(r + 1, "C") Then 'next sku

x = WorksheetFunction.Min(Abs(pinkcount), Abs(purplecount))
If x = 0 Then x = ""
Cells(r, "O") = x

pinkcount = 0
purplecount = 0

End If
Next r

End Sub

hth,
Ross
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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