Organizing for Success

 < Day Day Up > 

It can be tempting to throw every bit of code you need to complete a series of tasks into a single procedure, but it’s much more efficient and effective to write bite-sized procedures that do one task and then call the individual tasks from within a single main procedure. For example, if you wanted to create a program that wrote the contents of your daily sales worksheet to a database, saved the current file under a new name based on the date, deleted the contents of the sales worksheet, and saved and closed all open files, you might have a main program that looks like the following code:

Sub Main()
Call UpdateSQL()
Call ResaveWorkbook()
Call DeleteData()
Call SaveAndClose()
End Sub

The contents of the individual procedures are not a concern to the Main procedure. In fact, you can change them as often as you like without having to go in and mess around with all the code in the Main procedure. This type of routine has been a hallmark of programming for years, and it’s a practice you would do well to adopt.

In this chapter, you learned how to create Sub and Function procedures, the containers for your VBA code. Remember the big difference between the two: Sub procedures don’t return a result to the main program, but Function procedures do. And now that you know how to create those containers, you’re ready to start affecting Excel workbooks with your code. Chapter 6 starts you on your way by introducing the Application object.

 < Day Day Up > 

Microsoft Excel 2003 Programming Inside Out
Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))
ISBN: 0735619859
EAN: 2147483647
Year: 2006
Pages: 161

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: