+ Reply to Thread
Results 1 to 5 of 5

Compile Error in VBA Code

  1. #1
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    810

    Compile Error in VBA Code

    Hi,

    I am trying to teach myself how to use VBA and, to that end, I wish to write some code to calculate the standard deviation of a range of data to be selected by the user.

    Unfortunately I have stumbled at the first hurdle, receiving a Compile Error when trying to execute the code. Can someone please tell me what is wrong:
    Please Login or Register  to view this content.
    Thanks!
    Last edited by romperstomper; 06-22-2011 at 08:13 AM. Reason: code tags

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: Compile Error in VBA Code

    Hi Andrew,

    The Set keyword assigns an object reference to a variable or property. Volatility is declared as a literal (a double) so you will get a compile error. Also, it's not great to give your variables and procedures the same name - it gets confusing!

    Handling Application.InputBox in this situation is a little bit tricky: if the user cancels then the assignment will fail and the only way to counter this is with error handling.

    Please Login or Register  to view this content.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: Compile Error in VBA Code

    Hi Andrew

    You need to dim Volatility as Range, not Double as it is the range holding your data, not the resulting value

    Please Login or Register  to view this content.
    It will work fine without dimming Answer, and just having Msgbox display the result of StDev but I am guessing you want to use the result somewhere else
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Compile Error in VBA Code

    Please Login or Register  to view this content.



  5. #5
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    810

    Re: Compile Error in VBA Code

    Excellent, thank you all very much!

+ 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