Posted by Dom on .
I'm trying to write a sub that asks how much oil I made, and then gives me number of barrels needed to hold oil, cost of producing oil, and profit from oil. This is what I have so far, I'm not sure what to do.
Dim barrels As Variant
Dim tons As Integer
InputBox "How many tons of oil were produced?"
Barrels = Gallons / 10
cost = 0.38 * Barrels
profit = 0.27 * Barrels
MsgBox "" & Barrels & " barrels needed to hold milk"
MsgBox "Cost of producing oil = " & cost & ""
MsgBox "Profit of producing oil = " & profit & ""
sorry, the input box above is supposed to ask how many gallons* of oil were produced
First, the concept of sub programs is to isolate a particular calculation and make a "black box" which returns a "complicated" answer without having to worry about how it is done.
On the other hand, the UI (user-interface) belongs to a separate element of your program which might change due to user feed-back, requirements etc.
It is usually considered good program design to separate the two elements so that changing one does not necessitate the modification of the other.
As you go later into OOP (object oriented programming), the concept will be more clear.
Here you have combined the two design elements together. VBA will allow you to do this, but do bear in mind that in larger programs, this is not a good idea.
In the program, the concept is quite clear: input - calculations - output.
However, You have not defined the variable Gallons, and a value has not been assigned to be before first use. So the sub will not run properly even if it compiles.
You need to store the value of gallons of oil. The syntax (I believe) should be
Gallons=object.InputBox("How many gallons of oil")
where "object" is the object in which the sub resides.