option explicit

mikieprojects48

New Member
Joined
Jun 5, 2011
Messages
9
I'm new to vba and going through the Dummies for VBA book now, one of the example exercises is for CUBEROOT.

Sub CubeRoot()
Num = InputBox("Enter a positive number")
MsgBox Num ^ (1 / 3) & " is the cube root. "
The only way that I can get the macro to work is by removing the Option Explicit from the text window. I read that option explicit protects the code and if you put the letters dim in front of the lines the macro will work not able to get the right combination any help would be appreciated
mikie48
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Option Explicit forces us user to declare variable.
when 'm not sure I just decalare it for exmapel as as
dim num

Code:
[/FONT]
[FONT=Courier New]Option Explicit[/FONT]
[FONT=Courier New]Sub CubeRoot()
Dim Num As Integer
Num = InputBox("Enter a positive number")
MsgBox Num ^ (1 / 3) & " is the cube root. "
End Sub
[/FONT]
[FONT=Courier New]
 
Upvote 0
Option Explicit is a good thing and should not be disabled (IMHO). Having it enabled will allow VB to alert you when you are using a variable or constant that you did not declare in some way (usually using a Dim statement, but Private, Public, Static, Const, etc. are other ways to declare things). In your posted case, the code is using a Num variable that was not declared, so VB is warning you of that fact. Change your code fragment to this...

Sub CubeRoot()
Dim Num As Double

Num = InputBox("Enter a positive number")
MsgBox Num ^ (1 / 3) & " is the cube root. "

and your code will work fine. Note that I guessed at Double because I assumed floating point values were intended, but we could have used Integer or Long if whole numbers were what was expected.

Oh, and the reason using Option Explicit is a good idea... it will save you lots of debugging time when you create a variable named, say, firstline or Var01 and accidentally type them later on in your code as first1ine or VarO1. If you declare firstline and Var01 using Dim statements, and you have Option Explicit enabled, then you will be alerted to the mistyped variable names by VB. Without Option Explicit, it is possible your code will not crash, but rather continue on by calculation incorrect values based on those mistyped variable names. As I said, Option Explicit is a good thing to use (it save my butt many times across the years when I was still actively programming). By the way, the difference between the intended variable names and the mistyped ones may be more obvious in the font used here in this forum, but it will be harder to spot in the font used in the VB editor.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top