Public Variables

To make a variable available to all the procedures in all the VBA modules in a project, declare the variable at the module level by using the Public keyword rather than Dim. Here's an example:

Public CurrentRate as Long

Variable Naming Conventions

Some programmers name variables so that their data types can be identified just by looking at their names. Personally, I usually don't use this technique very often because I think it makes the code more difficult to read, but you might find it helpful.

The naming convention involves using a standard lowercase prefix for the variable's name. For example, if you have a Boolean variable that tracks whether a workbook has been saved, you might name the variable bWasSaved. That way, it is clear that the variable is a Boolean variable. The following table lists some standard prefixes for data types:

Data Type

Prefix

Boolean

b

Integer

i

Long

l

Single

s

Double

d

Currency

c

Date/Time

dt

String

str

Object

obj

Variant

v

User-defined

u

The Public keyword makes the CurrentRate variable available to any procedure in the project, even those in other modules within the project. You must insert this statement before the first procedure in a module. This type of declaration must also appear in a standard VBA module, not in a code module for a sheet or a UserForm.

0 0

Post a comment