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

Kevin Pooley

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
alan

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this code fail without volatile declaration?

From Dev

Why does cron silently fail to run sudo stuff in my script?

From Dev

Why does cron silently fail to run sudo stuff in my script?

From Dev

Why does my systemd unit fail to run on server reboot?

From Dev

Why does my code run into an infinite loop?

From Dev

why does my javascript code for changing(onclick) the div opacity fail?

From Dev

why does my javascript code for changing(onclick) the div opacity fail?

From Dev

Why does my NSMutableURLRequest fail?

From Dev

Why does this Haskell code fail?

From Dev

Why does this assembly code fail?

From Dev

Why does this multiprocessing code fail?

From Dev

Why does my program run without parameters inside the functions' parenthesis?

From Dev

Why does this Theano code run successfully without any errors?

From Dev

Why does my julia code run so slowly?

From Dev

Why does my Haskell code not appear to run in Parallel

From Dev

Why does Python run my code bottom to top?

From Dev

Why does my rake file code run every time?

From Dev

Why does this code throw an IndexError only when run on my CA?

From Dev

Why does my xprop- command fail on login, if I run it from ~/.profile?

From Dev

Why does `bash <command>` fail to run?

From Dev

Why does Option Explicit still allow implicit For loops in Visual Studio?

From Java

Why does assembly binding fail in my project?

From Dev

Why does my cross-compiling fail?

From Dev

Why does my redirected CORS request fail?

From Dev

Why does my rspec test fail?

From Dev

Why does my kernel fail to build?

From Dev

Why does my linq to sql query fail?

From Dev

Why does my pointer arithmetic fail in this array

From Dev

Why does my wifi fail to stay connected?

Related Related

  1. 1

    Why does this code fail without volatile declaration?

  2. 2

    Why does cron silently fail to run sudo stuff in my script?

  3. 3

    Why does cron silently fail to run sudo stuff in my script?

  4. 4

    Why does my systemd unit fail to run on server reboot?

  5. 5

    Why does my code run into an infinite loop?

  6. 6

    why does my javascript code for changing(onclick) the div opacity fail?

  7. 7

    why does my javascript code for changing(onclick) the div opacity fail?

  8. 8

    Why does my NSMutableURLRequest fail?

  9. 9

    Why does this Haskell code fail?

  10. 10

    Why does this assembly code fail?

  11. 11

    Why does this multiprocessing code fail?

  12. 12

    Why does my program run without parameters inside the functions' parenthesis?

  13. 13

    Why does this Theano code run successfully without any errors?

  14. 14

    Why does my julia code run so slowly?

  15. 15

    Why does my Haskell code not appear to run in Parallel

  16. 16

    Why does Python run my code bottom to top?

  17. 17

    Why does my rake file code run every time?

  18. 18

    Why does this code throw an IndexError only when run on my CA?

  19. 19

    Why does my xprop- command fail on login, if I run it from ~/.profile?

  20. 20

    Why does `bash <command>` fail to run?

  21. 21

    Why does Option Explicit still allow implicit For loops in Visual Studio?

  22. 22

    Why does assembly binding fail in my project?

  23. 23

    Why does my cross-compiling fail?

  24. 24

    Why does my redirected CORS request fail?

  25. 25

    Why does my rspec test fail?

  26. 26

    Why does my kernel fail to build?

  27. 27

    Why does my linq to sql query fail?

  28. 28

    Why does my pointer arithmetic fail in this array

  29. 29

    Why does my wifi fail to stay connected?

HotTag

Archive