Consulting

Results 1 to 12 of 12

Thread: Syntax error after compiling

  1. #1

    Syntax error after compiling

    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.

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

    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.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try it like this:

    [VBA]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[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Thread moved to appropriate forum.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Quote Originally Posted by anik18
    [vba]
    Sub CubeRoot()
    Num = InputBox(“Enter a positive number”)
    MsgBox Num ^ (1/3) & “ is the cube root.”
    End Sub
    [/vba]
    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
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  5. #5
    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

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Hey, I have worked it out. I forgot to enable the macro. Thanks a lot.

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    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.

  10. #10
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    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:
    [vba]Dim i As Integer
    Dim x As String
    Dim y As Integer

    'rest of code starts here[/vba]

    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.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  11. #11
    Thanks Randy.

  12. #12
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •