+ All Categories
Home > Documents > Tutorial and introductory exercise to Visual Basic (VBA ...

Tutorial and introductory exercise to Visual Basic (VBA ...

Date post: 16-Oct-2021
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
15
1 MANAGEMENT OF PRODUCTION AND INVENTORY SYSTEMS MION01 Tutorial and introductory exercise to Visual Basic (VBA) programming in Excel Introduction Programming macro’s in Excel is a convenient way to build your own customized decision support tools. A macro is essentially a computer program that can be controlled via Excel. It can be used to perform more advanced calculations that are difficult or even impossible to do in the actual spreadsheet. The programming language used by Excel is called Visual Basic. An advantage with using VBA programming in Excel is that it comes with Excel, and no additional compiler or software program is needed. In this introduction, we focus on how VBA/Macro programming in Excel can be used to implement more advanced inventory control models, and to create useful decision support tools. You are more than welcome to apply this in order to solve the assignments in the course, but it is not required! VBA Programming in general offers almost endless possibilities to build your own cool software applications, but we will here limit ourselves to very basic functionality. This tutorial consists of 4 parts: A general introduction to VBA programming in Excel in terms of excerpts from the book Wølk (2010) available in Appendix A. This provides a general introduction to VBA programming and how to get started. An illustrative inventory control problem to be implemented as an Excel/VBA program. A solution to this problem is provided in the Excel file titled “VBA program template - (R,Q) fillrate evaluation and optimization” available on the course website. This program is designed as a template that can be used for programming other inventory control models. The solution illustrates most of the basic VBA syntax that is needed to build programs of inventory models. By going through how the given program solves the problem at hand you will get a tutorial of applying VBA programming to solve inventory control problems. A small exercise which is based on extending the analysis of the illustrative control problem above. A shortlist of useful VBA syntax and commands. This can be seen as a complement to the Help function in the software, and the provided program template discussed above.
Transcript
Page 1: Tutorial and introductory exercise to Visual Basic (VBA ...

1

MANAGEMENT OF PRODUCTION AND INVENTORY SYSTEMS MION01

Tutorial and introductory exercise to Visual Basic

(VBA) programming in Excel Introduction Programming macro’s in Excel is a convenient way to build your own customized decision support tools. A macro is essentially a computer program that can be controlled via Excel. It can be used to perform more advanced calculations that are difficult or even impossible to do in the actual spreadsheet. The programming language used by Excel is called Visual Basic. An advantage with using VBA programming in Excel is that it comes with Excel, and no additional compiler or software program is needed. In this introduction, we focus on how VBA/Macro programming in Excel can be used to implement more advanced inventory control models, and to create useful decision support tools. You are more than welcome to apply this in order to solve the assignments in the course, but it is not required! VBA Programming in general offers almost endless possibilities to build your own cool software applications, but we will here limit ourselves to very basic functionality. This tutorial consists of 4 parts: A general introduction to VBA programming in Excel in terms of excerpts from the book Wølk

(2010) available in Appendix A. This provides a general introduction to VBA programming and how to get started.

An illustrative inventory control problem to be implemented as an Excel/VBA program. A

solution to this problem is provided in the Excel file titled “VBA program template - (R,Q) fillrate evaluation and optimization” available on the course website. This program is designed as a template that can be used for programming other inventory control models. The solution illustrates most of the basic VBA syntax that is needed to build programs of inventory models. By going through how the given program solves the problem at hand you will get a tutorial of applying VBA programming to solve inventory control problems.

A small exercise which is based on extending the analysis of the illustrative control problem

above. A shortlist of useful VBA syntax and commands. This can be seen as a complement to the Help

function in the software, and the provided program template discussed above.

Page 2: Tutorial and introductory exercise to Visual Basic (VBA ...

2

To study this material on your own I recommend that you follow the order above. To get the most out of studying the provided program you need to first recap the theoretical solution to the problem which is available, for example, in Axsäter (2006). The instructor led tutorial will focus on explaining how the provided program is constructed, and step by step how the given problem is solved. To benefit from that you need to prepare by reading up on the analytical solution to the problem so that you can recognize how this is implemented in the program. An illustrative inventory control problem We consider a basic single-echelon inventory system with constant replenishment leadtime, complete backordering, normally distributed customer demand, controlled by a continuous review (R,Q)-policy. The problem is to construct a VBA program that will: (i) compute the fillrate for a given policy (R,Q) and (ii) optimize R to minimize the inventory while meeting a specified target fillrate (i.e., a service constraint model). The program should be designed so that an arbitrary number of single-echelon systems can be evaluated in a single program run. The input data required for the analysis encompass: The mean, µ, and standard deviation, , of the demand per time unit The order quantity, Q (and for (i) above the reorder point, R) The replenishment leadtime, L.

This inventory problem is thoroughly analyzed in Axsäter (2006) pp. 91-94, and pp. 98-99. You should go through this analysis before continuing. When you have familiarized yourself with the theoretical solution of the problem you can continue to study the Excel template program “VBA program template - (R,Q) fillrate evaluation and optimization” available on the course website. This program contains an implementation of the theoretical solution. The program consists of an Excel worksheet named “Interface” and an underlying VBA program. The interface sheet is where indata is entered and where the results are displayed. You run the existing program by clicking on the button “Run”. The choice between evaluating the expected fillrate of an existing policy, and minimizing the reorder point given a specified target fillrate is done by setting the variable Choice to 0 or 1 respectively. The number of inventory systems (or items in the same system) you want to evaluate during one program run is chosen by setting the value of N appropriately. Currently N=1 and only a single system is considered. Test and see what happens in the sheet when you change the number, say to N=5. When studying the program, you should start by first running it once and see what happens. You run the program by clicking on the “Run” button in the worksheet named Interface. The next step is to open the Visual Basic Editor as explained in Appendix A. In the upper left side of the VBA editor you can see that the program consists of 4 modules: Public_variables: Contains declarations of all public variables. The values of public

variables can be changed anywhere in the program and they do not need to be declared as input and output parameters in subroutines and functions. It is convenient to use public variables but very dangerous for larger programs. Locally declared variables are generally preferred

Page 3: Tutorial and introductory exercise to Visual Basic (VBA ...

3

Main_program: Contains the code for the main program that is executed when the “Run” button in the worksheet “Interface” is clicked on.

Input_data_module: Contains the code for the subroutine Input_data. This subroutine reads the input data specified in the Excel sheet “Interface” and assign it to variables defined in the program.

Normal_demand_single_stage_R_Q: Contains the code for the functions and subroutines called by the main program to solve the problem.

When analyzing the program a recommendation is to do so by looking at the modules in the order specified above. The main logic is specified in the main program and the detailed calculations are performed in the functions and subroutines called by the main program in order to complete the task. Simple ways of stepping through the code line by line are available under the “Debug” tab found on the top of the VBA editor window. You can experiment with different ways to step through the code, but one simple way is as follows:

1) Double click on the module Main_program 2) Set a breakpoint by clicking on the gray border to the left of the line

Set interface = Worksheets("interface") The entire line should then be highlighted in dark red.

3) Run the program by clicking on the “Run” button in the Interface worksheet or by clicking on the green “play” icon directly below the “Debug” tab in the VBA Editor.

4) The program will execute and stop at the breakpoint which will be highlighted in yellow. To step line by line through the program you just press the key F8.

5) You can set as many break points you want, and you can run the program stepwise from one breakpoint to the next (instead of stepwise line by line) by pressing the key F5.

The code contains a lot of comments (text in green which starts with ') the first comment in the main program is: '********Different types of variable declarations************************ The comments are not executable code but simply explanations of what the code is currently doing. To document your code by writing explanatory comments about what the code is doing is a very good strategy. It makes it much easier for others (and yourself) to read and understand the code. Hopefully the comments in the current program are sufficient for you to read the code and understand what it does without too much effort. A small exercise – extending the analysis of the considered inventory problem Assume that the holding cost per unit and time unit, h, and the shortage cost per unit and time unit, p, are available. Modify the provided VBA program so that it can be used for evaluating the expected costs of the considered inventory system. The theoretical analysis of this problem is available in Axsäter (2006) pp. 103-105. In the resulting program you should be able to choose the option of evaluating the expected costs in the Excel sheet “Interface”.

Page 4: Tutorial and introductory exercise to Visual Basic (VBA ...

4

Shortlist of useful VBA syntax and commands This incomplete list includes some basic VBA syntax and commands. Good book references for someone who wants to learn more include Wølk (2010), Walkenbach (2004), and more recent editions of the same text. The list below should be seen as a complement to the template program which illustrates how most of the syntax works. To get help and information about how different VBA commands work the “Help” tab at the top of the VBA Editor (to the right) is very useful. It is recommended that it is used to get more thorough explanations of the items in the list below. If you have a command that you want to know more details about, a tip is to mark it in the code and then hit F1. This is a shortcut to the help function. Option Explicit: If entered at the very top of a module it prevents the code in that module to run

if it contains undeclared variables

Different types of loop statements: For-To-Next and Do-While-Loop

For integer_var = lower_limit To upper limit ‘Unconditional loop

Next

Do While statement=True ‘Conditional loop Loop

If-Then-Else-End If statements

If statement1=true Then ElseIf statement2=true Then ‘Optional condition level Else ‘Optional condition level Endif

WorksheetFunction.object : VBA contains a large number of WorksheetFunction objects, which can be thought of as small programs and specialized functions that can be very helpful in building the code. For example,

y= WorksheetFunction.NormDist(x, 0, 1, True)

assigns the value of the cumulative distribution function for a normal distribution with mean=0 and standard deviation=1 and the argument x, to the variable y. (y=P(Xx) when XN(0,1)).

A list of the available WorksheetFunction objects can be obtained by pressing the key F2 when the marker is in the VBA Editor window.

Int(number) and Fix(number) : Both Int and Fix remove the fractional part of number and return the resulting integer value. The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.

Abs(x): Returns the absolute value of the number x

Page 5: Tutorial and introductory exercise to Visual Basic (VBA ...

5

Exp(x): Returns the value ex

Log(x): Returns the natural logarithm of x

MsgBox: Displays a message box and (optionally) returns a value (see Help in VBA Editor for more details).

Now: Returns the current system date and time

Rnd: Returns a uniformly distributed random number between 0 and 1

Sqr(x): Returns the square root of the number x

Time: Returns the current system time

And: Logical and that can be used for combining conditions, i.e., If (x>5) And (x<10) Then

Or: Logical either or that can be used for combining conditions

Stop: Stops the program at the line where this command is found and opens the VBA Editor for debugging.

References

Axsäter S. Inventory Control, Second edition. Springer, New York, 2006

Walkenbach, J., Excel VBA programming for Dummies, Wiley Publishing, 2004.

Wølk, S., VBA Programming in Business Economics, DJØF Publishing, Copenhagen, 2010

Page 6: Tutorial and introductory exercise to Visual Basic (VBA ...

6

Appendix A Introduction to VBA programming in Excel

Excerpts from

Wølk, S., VBA Programming in Business Economics, DJØF Publishing, Copenhagen, 2010

Page 7: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 8: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 9: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 10: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 11: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 12: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 13: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 14: Tutorial and introductory exercise to Visual Basic (VBA ...
Page 15: Tutorial and introductory exercise to Visual Basic (VBA ...

Recommended