Why does my code run without 'Option Explicit' but fail with it?

风流意气都作罢 提交于 2020-01-05 14:12:10

问题


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

回答1:


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.




回答2:


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.




回答3:


  1. You need to declare variables before using it
  2. Where's End Sub for MercOne()?


来源:https://stackoverflow.com/questions/20014154/why-does-my-code-run-without-option-explicit-but-fail-with-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!