What is Option Base and Option Explicit ?


Ans :A) Option Explicit : 

☞ The "Option Explicit" is a statement that must appear in a module before any procedures.


☞ When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements.

☞ If you attempt to use an undeclared variable name, an error occurs at compile time.

☞ If you don't use the Option Explicit statement, all undeclared variables are set to Variant type by default , unless otherwise you specifies it's type.

Example1:

Option Explicit

Sub CHECK1()
Dim y As Integer
x = 10
'Here an error occurred because it is an undeclared variable Type. This Error will shown only when you use "Option Explicit" Statement.

'If you don't use the "Option Explicit" Statement No error will shown and all the Undeclared Variables((here 'X') Types are Set to Variant Type by Default.

y = 20
Cells(1, 1) = x + y
End Sub

Example2:
Sub CHECK2()
Dim y As Integer
x = 10
'Here we haven't used the "Option Explicit" Statement so that No error will shown and all the Undeclared Variable(here 'X') Types are Set to Variant Type by Default.
y = 20
Cells(1, 1) = x + y
End Sub

B) Option Base:
☞ Option Explicit makes the declaration of Variables Mandatory while Option Base used at module level to declare the default lower bound for array subscripts.
☞ For eg. Option Base 1 will make the array lower bound as 1 instead of 0.

Option Base 1

Sub TextArray()
Dim Mystr(5) As String

'Mystr(0) = "A" 'Can't Assign Value
Mystr(1) = "B"
Mystr(2) = "C"
Mystr(3) = "D"
Mystr(4) = "E"
Mystr(5) = "F"
'Mystr(6) = "G" 'Can't Assign Value
'Error Throw : Subscript out of range

StrText = Join(Mystr, "_")
MsgBox StrText
End Sub

Note : We Can not assign value as 2 or more to Option Base,
We will get error as "Compile Error : Expected : 0 or 1

1 Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
Previous Post Next Post