+ Reply to Thread
Results 1 to 14 of 14

Trouble defining variables in simple macro - newbie

  1. #1
    Registered User
    Join Date
    12-29-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Trouble defining variables in simple macro - newbie

    Hi there,

    I'm slowly going through VBA for dummies and a few other resources to try to learn VB. However,some of the code I've tried, even copying and pasting directly, gives me weird errors that for the life of me I can't debug. This is making learning a LOT harder for a newbie like me. :D

    This one I get fairly often so if someone could tell me what I'm doing wrong I'd appreciate it. In this case I'm trying to get a message to pop up to return the value in cell A1.


    Please Login or Register  to view this content.
    What I get is:
    Compile Error : Variable Not Defined

    Thanks guys!
    Last edited by poetstorm; 03-21-2012 at 10:42 PM. Reason: fix subject and code in msg

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Newbie to macro programming in need of help

    Hello,

    Please add code tags around your code. They are the # mark in the advanced reply. That makes the code easier to read.

    What your problem is not in the code, it's that you didn't declare what the variables mean.

    These two different approaches will give you the same result:

    Please Login or Register  to view this content.
    Or

    Please Login or Register  to view this content.
    When you use Option Explicit then you have to define every variable type or you will get this error. It is good coding practice to keep this statement as it saves memory and forces you to correctly declare variables. Also it will help catch a lot of stupid typos that you won't be able to catch easily.

    Good luck.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Newbie to macro programming in need of help

    Try:

    Please Login or Register  to view this content.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Newbie to macro programming in need of help

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    12-29-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Newbie to macro programming in need of help

    Quote Originally Posted by abousetta View Post
    Hello,

    Please add code tags around your code. They are the # mark in the advanced reply. That makes the code easier to read.

    What your problem is not in the code, it's that you didn't declare what the variables mean.

    These two different approaches will give you the same result:

    Please Login or Register  to view this content.
    Or

    Please Login or Register  to view this content.
    When you use Option Explicit then you have to define every variable type or you will get this error. It is good coding practice to keep this statement as it saves memory and forces you to correctly declare variables. Also it will help catch a lot of stupid typos that you won't be able to catch easily.

    Good luck.
    Thanks. I think I will try to keep the explicit option on. My problem is that VBA for dummies gives me code with that option turned off so I have tyo figure out what is not defined and fix it. Not easy for a newbie. :D

    If you don't mind, can you tell me what this does / means? I'm not familiar with the DIM command and not sure how that will make my code work in explicit mode. Thanks! I like to understand things rather than just sticking them in. It's how I learn.

    Please Login or Register  to view this content.


    Quote Originally Posted by Winon View Post
    Try:

    Please Login or Register  to view this content.
    Thanks! That way seems more compressed and efficient than what I had. I shall try all of these when I get home.



    Quote Originally Posted by royUK View Post
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    My apologies, I believe I fixed everything now. If not please let me know. Thanks everyone. I appreciate it very much. I'm new but determined. So please forgive my simple (and maybe stupid) questions. :D

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Trouble defining variables in simple macro - newbie needs hel;p

    Hi, there is a chapter on declarations in vba for dummies (that's where I started myself). It also goes into detail on how much memory type of declaration uses. If you don't declare then Excel will try and guess what the variable type is. No declaration = variant, which means that even for simple things it will take up more memory than needed.

    Let me know if you still have more questions and don't forget to PM Roy so he can delete his post.

    Good luck.

    abousetta

    P.S. I still try to find time to read that book from cover to cover because without the basics, we are bound to fall into simple stupid mistakes that are covered in vba 101
    Last edited by abousetta; 03-20-2012 at 11:38 PM.

  7. #7
    Registered User
    Join Date
    12-29-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Trouble defining variables in simple macro - newbie needs hel;p

    Thanks

    Can someone let me know why this doesn't work? I'm using explicit mode...but I believe I have defined everything completely. So why won't it run? It still says "not defined" Thanks again.

    Please Login or Register  to view this content.
    Edit: I have tried changing the workbook and the worksheet to just 1. I have also tried renaming my file without spaces. I'm just stymied and I run into this with all my programs now. What am I doing wrong? :D
    Last edited by poetstorm; 03-21-2012 at 09:36 PM.

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Trouble defining variables in simple macro - newbie needs hel;p

    Is this being run in the same workbook or another workbook?

    What is the declaration for Contents? You should be getting an error message that it was not defined.

  9. #9
    Registered User
    Join Date
    12-29-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Trouble defining variables in simple macro - newbie needs hel;p

    It's in a workbook called cell_values.xlsm. I am getting a "not defined" error but it highlights different parts of my macro when the error comes up depending on how I fiddle with the sheet and workbook descriptions.

    The Contents part I'm literally taking from the book but the darn thing doesn't work. Code I completely copy from the book doesn't work either, probably because I have Explicit turned on. Here's an example straight from the book that I can't get to work no matter what I do:

    Please Login or Register  to view this content.
    So I'm trying to learn what to do to modify it to work in that mode.

    Not sure what you mean regarding declaration but it was supposed to tell me the value of a cell. Sorry, I'm kinda lost in the woods here. When the textbook examples spur errors I have no idea where to go from there. LOL

    The problem is the last few macros I tried gives me the same undefinied errors, so I have to be doing something wrong in my programming syntax somewhere.

  10. #10
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Trouble defining variables in simple macro - newbie needs hel;p

    Hi poetstorm,

    (“Sheet1”).Range(“A1”).
    The (Sheet1) and (A1) are not Quotation marks recognised by VBA. You should change it form “Sheet1” to ("Sheet1") and ("A1")and then copy and use the code below:

    Also note the correct Quotation marks.

    Please Login or Register  to view this content.
    Hope it helps

  11. #11
    Registered User
    Join Date
    12-29-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Thumbs up Re: Trouble defining variables in simple macro - newbie needs hel;p

    Quote Originally Posted by Winon View Post
    Hi poetstorm,



    The (Sheet1) and (A1) are not Quotation marks recognised by VBA. You should change it form “Sheet1” to ("Sheet1") and ("A1")and then copy and use the code below:

    Also note the correct Quotation marks.

    Please Login or Register  to view this content.
    Hope it helps
    Well what do you know, it was mainly a simple matter of quotes. Go figure. (Would have been nice of the book's author to tell me I neded that "Dim" line in there too.)

    Thanks so much for the help, both of you. I have been pulling my hair out and ready to give up on teaching myself VBA. Thanks again! (I may need to find myself a new book too LOL)

  12. #12
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    re: Trouble defining variables in simple macro - newbie

    You are welcome! Glad I could help.

  13. #13
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    re: Trouble defining variables in simple macro - newbie

    Good catch Winon. I have had this problem with quotation marks before. You can't write the code in word, etc. and paste because of this problem.

  14. #14
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    re: Trouble defining variables in simple macro - newbie

    @ abousetta,

    Thanks abousetta. I was just lucky to spot the "mistake". What you gave him was more than perfect and at first I could also not understand why he would have a problem with it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1