Summing in VBA

Vlad21

New Member
Joined
Jul 10, 2014
Messages
11
Hi guys,


So I've been using excel for about a year now and this is my first attempt at creating a macro. Unsurprisingly it's not working out to well.


I'm trying to create a macro that will sum our customers demand for a certain part until it's equal to our stock value and then return the date that our stock will last till.
Here's an example of the way my table is laid out:

Part NumberDescriptionPO Date Needed Delivery DateQTY NeededStock QTYProduction QTYFirst Short DatePlanning Start DatePast Due7/6/20147/13/20147/20/2014
14926PIN PO-12846 1413010/12/2014 1011
15242CUPPO-89647 3010Planning Start Date 1011
2081TENSION RODPO-36987 11135010/12/2014 1011
2022SHAFTPO-16487 4150 1210
2050CELLPO-69587 5120 2012
2138GEAR PO-24761 4110 1210
2148FILTER PO-64975 14400 1630
2001BACK SUPPORT PO-36548 11020 9011
2431WEBPO-54877 9015 7110

<colgroup><col><col><col><col><col><col><col><col><col span="2"><col><col><col span="2"></colgroup><tbody>
</tbody>


Don't mind the empty cells (Date Needed, Delivery Date and Planning Start date) other info will go here later on. Right now I'm trying to fill the first short date column with the date our stock will last till. Here is the VBA code I have right now:

Sub first_short_date()
Dim stock As Integer
Dim row_count As Integer
Dim column_count As Integer
Dim demand_sum As Integer



row_count = 2 'Starting point of data under title

Do Until Cells(row_count, 1).Value = "" 'Stops when data ends

stock = Cells(row_count, 7) 'Stock data located in Column B
column_count = 11 'Beginning of demand data
demand_sum = 0 'Reset demand

Do Until demand_sum = stock 'Stops when stock reaches demand

demand_sum = Application.Sum(Range(Cells(row_count, 11), Cells(row_count, column_count))) 'Progressivly sums demand
column_count = column_count + 1

Loop

Cells(row_count, 9) = Cells(1, column_count - 1).Value 'Returns the corresponding date to when demand=stock
row_count = row_count + 1

Loop

End Sub


As you can see from the table above this works for the first 3 rows (I know that it's not returning the date for row 2 but this is because the stock is zero. I plan on adding an if statement to fix this later). When it gets to the 4th row it returns, run time error "1004": Application-defined or Object-defined error.
I ran the code line by line with the debug and it seems to get stock in an infinite loop at this part:

Do Until demand_sum = stock 'Stops when stock reaches demand

demand_sum = Application.Sum(Range(Cells(row_count, 11), Cells(row_count, column_count))) 'Progressivly sums demand
column_count = column_count + 1

Loop

At first I thought that maybe my stock was exceeding the demand but this table continues on for a while and the demand is much greater then the stock.

If you guys can offer any advice on this I would greatly appreciate it.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Ya that works, thank you.
I've been pulling my hair out trying to figure this thing out.
I'm new to programming in general so I tend to miss things like that, thank you again.
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,020
Members
449,203
Latest member
tungnmqn90

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