+ Reply to Thread
Results 1 to 7 of 7

referring to user selected range in VBA function

  1. #1
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    referring to user selected range in VBA function

    Hi,

    I'm relatively new to VBA and still learning. Just as an example I would like to replicate the excel SUM function in VBA.

    my function in "Sheet1" code window looks like this...

    Please Login or Register  to view this content.


    this doesn't seem to work...in excel i tried to call the function sumexample like this....=sumexample(A1:B2)

    i get an error "#NAME?"...what am i doing wrong...how should it look?

    thanks

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: referring to user selected range in VBA function

    Hi Xelhelp,

    You have to put user-defined functions in standard modules, not within sheet modules. In the VB Editor click Insert > Module and put the code there. You should then be able to use your formula in the worksheets.

  3. #3
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: referring to user selected range in VBA function

    Hi Paul,

    I tried to do what you said...i still get an error when i try to use the function in an excel spreadsheet...this time the error is "#VALUE!"

    thanks

  4. #4
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: referring to user selected range in VBA function

    hmmm...ok so i just changed WorksheetFuntion.Sum(numbers) to Application.WorksheetFunction.Sum(numbers) and it seems to work now....

    why was it necessary to add the Application prefix? and what is the difference between sheet module and standard module?

    on a side note..could you please state the differences between functions, procedures and sub-procedures?

    thanks

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: referring to user selected range in VBA function

    Rather than having me explain the differences, there's volumes of information within this forum and by doing simple searches on Google.

    http://www.techbookreport.com/tutorials/excel_vba1.html
    http://www.cpearson.com/excel/writin...ionsinvba.aspx
    http://www.xlpert.com/partE.htm
    etc..

    Without 'Application.' before 'WorksheetFunction.Sum' VBA apparently doesn't know what object you're referring to.

    http://www.cpearson.com/excel/callin...ionsinvba.aspx

    A sheet module is typically used to store code that relates to that worksheet and events within that worksheet. A standard module is used to store code that you typically use from any worksheet in the workbook (or reference from other workbooks). There is also the ThisWorkbook module, which typically is used to store workbook-level event code (such as a change event that you want to occur on all worksheets, not just one).

    This is a very basic explanation, to be sure, but there's a ton of information out there on the subject. (Never memorize anything that you can look up.™)


  6. #6
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: referring to user selected range in VBA function

    thanks for you help Paul. I'm def. learning a lot on this forum.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: referring to user selected range in VBA function

    you also spelled worksheetfunction wrong the first time.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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