Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

3.13 How To Clean Your Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

If you have a variable named "myVar", you can get its value by:

?myVar

You can also type in a statement and it is executed when you press enter:

ActiveCell.Value = 123

You can also put statements in your code that write to the Immediate window:

Debug.Print "The value of myVar is " & myVar

3.13 How To Clean Your Code


For some reason, Visual Basic modules seem to grow in size beyond the amount of code that you
put into a module. It appears that Visual Basic is still retaining copies of old, deleted code. To
eliminate this excess code, you can use Rob Bovey's code cleaner that will clean your code. It is
available at

http://appspro.com/Utilities/CodeCleaner.htm

Basically, the code cleaners store the code in an ASCII file, delete the module, and then recreate
it with the code stored in the ASCII file. Code that has not been cleaned has been know to be
unstable and cause crashes.

3.14 Useful Module Level Statements


The following statements, placed at the top of a module, can be very useful:

Option Explicit

The above statement insures that all variables that you use in the module must be declared with a
Dim statement. This insures that you do not misspell a variable's name and accidentally create a
new variable.

Option Compare Text

The above statement makes any text comparison in the module case insensitive.

Option Private Module

The above statement prevents subroutines and functions in a module from being visible to the
macro list available from the Tools menu. They can however be referenced by other subroutines
and functions in other modules in the workbook.

3.15 Recovering Code From A Corrupt File

30
Andrew Baker has created a free program called a workbook rebuilder available at his site:

http://www.vbusers.com

The direct link to the download is:

http://www.vbusers.com/downloads/download.asp#item2

3.16 Naming Your Visual Basic Projects


In Excel Visual Basic, each workbook is considered a "project". When you click on the Project
Explorer button in the visual basic editor, you will see a listing of each project or workbook. The
default name of each project is "VBAProject". To change this name to a more descriptive name,
do the following:

In the Project Explorer, click on the project line, the one with the name of the
workbook.

Click on the properties button to display the properties window.

Change the name to the project to a more descriptive name. Spaces are not
allowed. Use underscores instead of spaces.

3.17 Docking Windows In The Visual Basic Editor


Assuming that you inadvertently undocked these windows, you'll first need to turn on the
docking property for each of the desired windows. Right click anywhere inside the desired
window & click Dockable on the shortcut menu, then drag the window to the side you want it
docked to. You can also turn docking on or off using (from the VBE) Tools, Options, Docking
Tab.

To dock the Explorer window, move it left until the thick gray line around it turns into a thin
dotted one. It should attach to the left side of the screen.

To dock the properties window move it left and down almost until it disappears off the lower left
part of your screen. At this point the gray line should change again into a dotted one. Let go and,
hopefully, it should return to normal. If it does not on the first try just double click on the
properties window blue menu bar and try again… and again... and again...

The key thing is to watch the window outline as you drag. The thick gray outline will leave the
window floating in the middle of the screen, on top of any undocked windows. A thin dotted
outline will dock the window at an edge or against another docked window.

However, the final docking place is not quite as haphazard as it might seem. If you drag the thin
outline left and right across the bottom of the screen (keeping it low enough to avoid the thick
outline), you will see its shape go through a number of transitions that indicate how it will dock.
It is best to have a maximized code module open while you do this. Otherwise, the gray border is
difficult to see against the gray window background.

31
A slightly taller outline indicates that it will dock against the left or right edge of the screen or
left or right edge of another docked window. A slightly flatter short outline indicates it will dock
under another docked window or against the bottom of the screen. If you drag the outline really
low, it will widen and indicate that it will cover the entire bottom of the screen. These changes
are fairly subtle, so you really have to look carefully. In Excel 2000, these changes are more
exaggerated and easier to see,

3.18 Books On Learning Windows API


The Application Programmer's Interface (API) is a library of hundreds of windows functions that
require exact programming. One mistake can render your system useless. The definitive book
on this library is Dan Appleman's, "Visual Basic 5.0 Programmer's Guide to the WIN32
API." It is extensive. Another good book that encapsulates some of these functions in classes
with examples on CD is "VBA Developer's Handbook," by Ken Getz and Mike Gilbert. Of
course spread across the web at microsoft.com and many of the VB sites you'll find examples of
using the API. Another good book which makes use of the API is "Visual Basic Graphics
Programming," by Rod Stephens.

3.19 Disabling Macro Virus Check


In case you are wondering, it can't be done. Doing so would make the virus protection. You
can however, disable the warning screen that appears if you don't want to be told that workbooks
contain macros. This is an option on the warning screen.

3.20 Translating 123 Macros To Excel Macros


There are not any tools (other than consultants <g>) that translate macros from 1-2-3 to Excel.
Even if there were, the translation would never be as good as determining exactly what they
should do and then write the VBA code from scratch. If you simply try to do a line-by-line
translation of a complex 1-2-3 macro, you probably won't be taking full advantage of the features
available in Excel and VBA.

3.21 Converting Lotus 1-2-3 Macros To Visual Basic


For information on converting 1-2-3 macros go to the following Microsoft web site:

http://support.microsoft.com/support/kb/articles/q148/2/40.asp

That informs you about an MS Application Note "WE1277:XL7:Visual Basic Equivalents for
Lotus Macro Commands". Click on the WE1277.EXE to download this file. When you
download an run it, It creates a word documentation that you can then read.

3.22 The Equivalent Of A Lotus 1-2-3 Macro Pause


In Lotus, you could pause your code so that the user could modify a cell before the macro
resumed. In Excel, you would use Application.InputBox or InputBox to get user input, and

32
then have your code modify the cells. Please see the topics on Application.InputBox or
InputBox for examples of how to use these features.

33

You might also like