I am constructing my first major program in VBA (With some help from SO). The beginning of it is below, and I am adding blocks of code in as structured a way as possible. Having read about the importance of using 'Option Explicit' though, I inserted it and started to declare all my variables. Running the program gave - 'Compile Error: Variable not Defined'.
I have tried deleting and re-entering declarations in case the problem was caused by me adding 'Option Explicit' after starting on the code, as well as removing the UDF and adding that back in, but without success.
Commenting out 'Option Explicit' does away with the error message and the subsequent blocks of code run exactly as designed.
EDIT:I should have said that the error always occurs with 'iLoopControl'.
Option Explicit
'UDF to roll a number of Dice of specified size and total them
Function Dice(NoOfDice, DiceSize)
Dice = 0
For iLoopControl = 1 To NoOfDice
Dice = Dice + WorksheetFunction.RandBetween(1, DiceSize)
Next
End Function
Sub MercOne()
Randomize
Dim Merc(86)
Dim Temp As Integer, TechLevel As Byte, ArmOfService As Byte
Dim Year As Byte, YearCount As Byte
Dim GenAssignment As Variant, UnitAssignment As Variant
Dim SpecAssignmentSwitchEnd As Byte, Roll As Byte
Dim Rank As Variant, NoOfDice As Variant, DiceSize As Byte
Dim GenAssignmentSwitchInt As Byte, GenAssignmentSwitchOff As Byte
Dim CharacterNumber As Long, iLoopControl As Long
End Sub
forMercOne()
?Option Explicit
means you must explicitly declare all variables and their types before using them. If you don't include theOption Explicit
declaration, then VBA will just createVariant
type variables whenever it encounters a new identifier.So for example, you need to include
Dim iLoopControl as Integer
if you have declaredOption Explicit
:If you didn't include
Option Explicit
then you could just useiLoopControl
without using theDim iLoopControl as Integer
to declare it.I applaud you for using
Option Explicit
! Now you have to learn to use it well...You have to declare a variable before using it. You use
iLoopControl
in your functionDice
before declaring it. VBA will assume (absent Option Explicit) that you were just lazy, and create a Variant variable for you. But if you misspelled the variable name, it would create a new one - and debugging would be really difficult because you won't understand why values are wrong...As an aside, it is a good idea to avoid
Variant
in general, except when you know the variable can contain different types, when you will use it in aFor Each
loop, or when it is an optional parameter in a function. It is slower than a "regularly typed" variable and takes more space. In larger programs that can make a difference. And if you do want a Variant, you don't need to sayAs Variant
since that is implied.One more point about your code: you declare
iLoopControl
in the scope of aSub
. That means it is unknown (and "undeclared") when you are not in that sub. Not even when you are inside a function called from that sub. The only variables that can be "seen" inside a Sub/Function are the ones that are declared at the module level (outside of sub/function), or within the sub itself.