Why does my code run without 'Option Explicit&

2019-08-13 14:16发布

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

3条回答
Evening l夕情丶
2楼-- · 2019-08-13 14:43
  1. You need to declare variables before using it
  2. Where's End Sub for MercOne()?
查看更多
Luminary・发光体
3楼-- · 2019-08-13 14:45

Option Explicit means you must explicitly declare all variables and their types before using them. If you don't include the Option Explicit declaration, then VBA will just create Variant type variables whenever it encounters a new identifier.

So for example, you need to include Dim iLoopControl as Integer if you have declared Option Explicit:

Option Explicit    
Dim iLoopControl As Integer
For iLoopControl = 1 To NoOfDice
   Dice = Dice + WorksheetFunction.RandBetween(1, DiceSize)
Next

If you didn't include Option Explicit then you could just use iLoopControl without using the Dim iLoopControl as Integer to declare it.

查看更多
神经病院院长
4楼-- · 2019-08-13 15:06

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 function Dice 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 a For 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 say As Variant since that is implied.

One more point about your code: you declare iLoopControl in the scope of a Sub. 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.

查看更多
登录 后发表回答