PDA

View Full Version : Syntax error after compiling



anik18
12-29-2008, 12:06 AM
I tried to enter the code below in VBA excel 2003. I filled this module section and there was an error after compiling. The error said "Syntax error". Plz help me how to fix it and i am new to VBA so i am learning how to do it. Thanking you.


Sub CubeRoot()
Num = InputBox(“Enter a positive number”)
MsgBox Num ^ (1/3) & “ is the cube root.”
End Sub


Edit Lucas: VBA tags added to code. Anik, when you post code...select it and hit the vba button to format it for the forum.

lucas
12-29-2008, 12:05 PM
Try it like this:

Option Explicit
Sub CubeRoot()
Dim Num As Integer
Num = InputBox("Enter a positive number:", "Inputbox")
MsgBox Num * (1 / 3) & " is the cube root."
End Sub

lucas
12-29-2008, 12:11 PM
Thread moved to appropriate forum.

CreganTur
12-30-2008, 08:55 AM
Sub CubeRoot()
Num = InputBox(“Enter a positive number”)
MsgBox Num ^ (1/3) & “ is the cube root.”
End Sub



Welcome to the forum- it's always good to see new members.

As Lucas pointed out, the syntax error should occur because you never declared the variable Num that you are using.

Always Dim a variable before you use it- this create the variable and prepares it to be used. This is also where you declare what data type you want the variable to hold.

Also, you should notice in Lucas' code that there is an extra delcaration: Option Explicit. This forces you to declare all of your variables by throwing an error whenever you try to use an undeclared variable. This is a very, very good thing that will keep you from making some crippling mistakes. To be sure this is on, go into your VBIDE. Click on Tools->Options, and make sure that "Require Variable Declaration" has a check mark next to it.

HTH:thumb

anik18
12-30-2008, 03:04 PM
Thanks Lucas and CreganTur. I thought i was supposed to get Dialog Box for this code. See attachment for example of dialog box and codes. What is the full form of "Dim" given in code. Anyway thanks for some help.

Anik

lucas
12-30-2008, 03:05 PM
You're welcome. Be sure to mark your thread solved using the thread tools at the top of the page if you have your solution.

anik18
12-30-2008, 03:18 PM
Hey, I have worked it out. I forgot to enable the macro. Thanks a lot.

lucas
12-30-2008, 03:28 PM
Anik18, If this is school work you shoud state that up front. Please read our FAQ on homework.

It's not that we won't help you but we want you to learn so we will try to give you the help you need without actually doing your work for you.

One thing I would point out is that your first post...the " " marks were not being recognized by the visual basic editor when I pasted them it. It was like you had written this in Word or some other text editor and then pasted it in. If you do that you should use notepad.

anik18
12-30-2008, 04:13 PM
I'm trying to look at example first then i will work on my own. And one more is it is not school work. Self learning to make a project for our company. I'm learning not giving it up.

CreganTur
12-31-2008, 06:32 AM
What is the full form of "Dim" given in code.

Dim is short for Dimension- you are dimensioning the variable into the sub or function where it is created. This creates the memory space for the variable's values.

You must Dim all variables you want to use up front. Generally this is the first thing in most procedures- it's known as the declaration section, because the more common term for instantiating variables is to declare them (meaning most programmers will talk about declaring variables, not dimensioning them... don't know why... I digress...)

So, if you are writing a program where you know you will need three variables (i, x, and y) you declare them up front like this:
Dim i As Integer
Dim x As String
Dim y As Integer

'rest of code starts here

If you need to know more about data types, then you should search VBA help for "data type"

If you're having trouble learning on your own, then I would highly reccomend that you look into VBAX's Excel VBA training- click on the word Certifiable in my signature. It's a great program for a beginner to take.

anik18
01-02-2009, 06:32 PM
Thanks Randy.

Artik
01-03-2009, 04:47 PM
Knock,knock ;)
I came on about InputBox.
Beside to the InputBox function, there is InputBox method (Application.InputBox). It allows you to control the data entered. Look at Excel's Help.