+ Reply to Thread
Results 1 to 13 of 13

Calling a function without assigning it to a variable cause an error.

  1. #1
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Calling a function without assigning it to a variable cause an error.

    I have a function I tested with this procedure
    Please Login or Register  to view this content.
    which worked just fine. But this test for another function gives a compile error
    Please Login or Register  to view this content.
    unless I revise the call to
    Please Login or Register  to view this content.
    I appreciate it if someone could tell me why the second test is causing an error.

    Thanks

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Calling a function without assigning it to a variable cause an error.

    Try changing to :
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calling a function without assigning it to a variable cause an error.

    Thanks for the reply but I'd like to understand why the first test works and the second test fails. As I said in my OP assigning the function to a variable fixes the problem.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Calling a function without assigning it to a variable cause an error.

    What is the code for SetFillColor.
    I notice that the first bit of code is passing one argument to the routine, while the second bit of code is passing two arguments.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calling a function without assigning it to a variable cause an error.

    I'm including both functions, they are very similar.
    Please Login or Register  to view this content.
    And here's the one that Excel won't compile without the assignment.
    Please Login or Register  to view this content.
    Apart from understanding why the first test works and the second fails I've decided that the second function should return a boolean value signaling success or failure. If it fails it may be possible to recover from it. (Its part of a bookkeeping application for my daughter and it should work as expected.)

    Its been a long time since I programmed in Excel and its a fairly steep re-learning curve, if you have the time I would like to know what the problem is.

    Thanks for your time.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Calling a function without assigning it to a variable cause an error.

    It's is a syntax thing.

    If you are returning a value from a function then you need the parentheses.

    If you are calling a function but not returning a value you have 2 options.
    Please Login or Register  to view this content.
    If you wanted to return a boolean value indicating success/failure you could try something like this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calling a function without assigning it to a variable cause an error.

    If you look at the start of the thread you will see that my question is not about how to call and return a value from a function, but why the second call shown causes a compile error and the first does not.

    Thanks

  8. #8
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calling a function without assigning it to a variable cause an error.

    If I can figure out how to do it, I'm going to mark this thread as solved. As a test I changed the function and the unassigned call worked.
    Please Login or Register  to view this content.
    First I tried changing the procedure to a subroutine but Excel still insisted that I assign the call. Changed the function requirements to one parameter and Excel was happy. Still don't know why Excel insists on assigning the call if there's more than one parameter, but at least I know what was causing the error.

    Thanks to all who responded. Now if I could just figure out how to mark this thread as [SOLVED]...

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Calling a function without assigning it to a variable cause an error.

    You appeared to overlook the answer you were given.

    In VBA, you only use parentheses when calling a function if you use the Call statement or you use the returned value. Otherwise you should not use parentheses even if you only have one argument - although it will generally work with data types, it will fail if you need to pass an object type, such as a Range or Worksheet.
    Remember what the dormouse said
    Feed your head

  10. #10
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calling a function without assigning it to a variable cause an error.

    romperstomper
    Obviously, there more to it than your post indicates.
    Please Login or Register  to view this content.
    Ran just fine.

    Now if someone would be so kind as to tell me how to mark a thread as [SOLVED].

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Calling a function without assigning it to a variable cause an error.

    You are not passing an object there because you didn't use Set:
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    09-13-2013
    Location
    Vernon, Canada
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calling a function without assigning it to a variable cause an error.

    OK, thanks.

  13. #13
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Calling a function without assigning it to a variable cause an error.

    Quote Originally Posted by gvag View Post

    Now if someone would be so kind as to tell me how to mark a thread as [SOLVED].




    Please select Thread Tools from the menu link above and mark this thread as SOLVED,as per Forum Rule #9.


    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Problem with calling another macro (Error 450 and " Expected variable or procedure")
    By Roxner in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-07-2013, 04:59 AM
  2. [SOLVED] Compile error when calling function in sub
    By g8r777 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-05-2012, 11:26 AM
  3. Syntax Error calling StDev Function
    By ccolton in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-10-2011, 05:53 PM
  4. Assigning variable values using the Evaluate() function
    By gladst_j in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-09-2011, 04:51 AM
  5. calling function with variable amount of results
    By meisi in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-01-2008, 06:28 PM

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