constant vba code example

Example 1: excel vba constant

' Constants are Private by default. 
Const MyVar = 459 
 
' Declare Public constant. 
Public Const MyString = "HELP" 
 
' Declare Private Integer constant. 
Private Const MyInt As Integer = 5 
 
' Declare multiple constants on same line. 
Const MyStr = "Hello", MyDouble As Double = 3.4567

Example 2: excel vba how to declare a global variable

'In VBA, GLOBAL variables must be declared ABOVE all procedures in a module:

Public MyGlobalString As String				'<-- global to ALL modules
Private MyGlobalLong As Long				'<-- global to THIS module (Private)
Public Const HELP As String = "Press F1"	'<-- global to ALL modules

Sub Add(a, b)
	MyGlobalLong = a + b
End Sub

Call Add(7, 2)			
MsgBox MyGlobalLong		'<--displays: 9
MsgBox Help				'<--displays: Press F1

'
'
'

Tags:

Vb Example