VBA Keywords: Public


A Public Variable

A variable is referred to as public if it can be accessed by code either from within the same file (the same module) where it is declared or from code outside its module. To declare a public variable, instead of Dim, you use the Public keyword. Here is an example:

Option Explicit

Private LastName As String
Public FullName As String

Sub Exercise()
    Dim FirstName As String
    FirstName = "Patricia"
    LastName = "Katts"
    FullName = FirstName & " " & LastName
End Sub

As a reminder, a public variable is available to code inside and outside of its module. This means that you can create a module, declare a public variable in it, and access that variable in another file (module) where needed.



Home Copyright © 2009-2016, FunctionX, Inc.