Table of contents
Collapse the table of content
Expand the table of content

Private Statement

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Used at module level to declare privatevariables and allocate storage space.


Private [ WithEvents ] varname [ ( [ subscripts ] ) ] [ As [ New ] type ] [ , [ WithEvents ] varname [ ( [ subscripts ] ) ] [ As [ New ] type ]] . . .

The Private statement syntax has these parts:

WithEventsOptional. Keyword that specifies that varname is anobject variable used to respond to events triggered by anActiveX object. WithEvents is valid only inclass modules. You can declare as many individual variables as you like using WithEvents, but you can't createarrays with WithEvents. You can't use New with WithEvents.
varnameRequired. Name of the variable; follows standard variable naming conventions.
subscriptsOptional. Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscriptsargument uses the following syntax:
[ lowerTo ] upper [ , [ lowerTo ] upper ] . . .
When not explicitly stated in lower, the lower bound of an array is controlled by the OptionBase statement. The lower bound is zero if no OptionBase statement is present.
NewOptional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsicdata type, can't be used to declare instances of dependent objects, and can't be used with WithEvents.
typeOptional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported),Date, String (for variable-length strings), String * length (for fixed-length strings),Object, Variant, a user-defined type, or an object type. Use a separate Astype clause for each variable being defined.

RemarksPrivate variables are available only to the module in which they are declared. Use the Private statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer:

Private NumberOfEmployees As Integer 

You can also use a Private statement to declare the object type of a variable. The following statement declares a variable for a new instance of a worksheet.

Private X As New Worksheet 

If the New keyword isn't used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it's assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object. If you don't specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default. You can also use the Private statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs. When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Variant variables are initialized toEmpty. Each element of a user-defined type variable is initialized as if it were a separate variable.

Note When you use the Private statement in a procedure, you generally put the Private statement at the beginning of the procedure.


This example shows the Private statement being used at the module level to declare variables as private; that is, they are available only to the module in which they are declared.

Private Number As Integer ' Private Integer variable. 
Private NameArray(1 To 5) As String ' Private array variable. 
' Multiple declarations, two Variants and one Integer, all Private. 
Private MyVar, YourVar, ThisVar As Integer 
© 2018 Microsoft