+ Reply to Thread
Results 1 to 6 of 6

Using a Boolean function within an If statement: Argument not optional error message

  1. #1
    Registered User
    Join Date
    12-07-2011
    Location
    minneapolis, mn
    MS-Off Ver
    Excel 2007
    Posts
    11

    Using a Boolean function within an If statement: Argument not optional error message

    Hello

    I'm sure this is going to be a huge derp moment (total programming novice here), but I have a question about how to properly nest a Boolean within an If statement (... within an If, within a Loop, within a Function, heh).

    What I have written here looks (to my eyes) exactly like the 5 or 6 examples I was able to find via google, but when I step through my Sub it returns a "Compile error: Argument not optional" message box whilst highlighting the text I've written in red below. Is the If statement wrongly constructed, or is it the Boolean Function that's got an error?

    The overall goal of this particular function is to copy specific cells in a row from sheet1 to sheet3, but only if the values of two of those cells (SectionNumber and AudCrit) are not already seen -- together in the same row -- in columns B and C on sheet3.

    The Larger Function:
    Please Login or Register  to view this content.
    The Boolean Function is:
    Please Login or Register  to view this content.
    Thanks for any help you can give me.
    Last edited by elfsprin; 12-16-2011 at 04:13 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Using a Boolean function within an If statement: Argument not optional error mess

    I'm not sure but I don't think you need the second ByVal beside AudCrit. Also, you should declare the type of variable that AudCrit is in that line.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Using a Boolean function within an If statement: Argument not optional error mess

    elfsprin,

    That error happens when a function is missing an argument to complete correctly. The function you created requires three arguments: SectionNumber, AudCrit, and OccupiedRow. In the line that's giving you an error, you have only fed the function 2 arguments:

    Quote Originally Posted by elfsprin View Post
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Registered User
    Join Date
    12-07-2011
    Location
    minneapolis, mn
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Using a Boolean function within an If statement: Argument not optional error mess

    Excellent, thank you! Adding the missing argument OccupiedRow allows me to step past that line without an error. Derp.

    If I might ask two small additional questions while I have your attention:

    1. Whenever I've stepped through my Subs and come to a line where I'm calling a function, the VBA editor has been taking me to that function within the module and stepped through each of its lines as well (before adding this Boolean to further refine my outputs, I had one Sub with three functions being called). However, for this Boolean function CheckForExistence1, the editor is just waltzing past that line in FindValue2 without taking me down to CheckForExistence1 and stepping through it. Is that because this is a function within a function / can I expect that behavior to be replicated in the future?

    2. Does the "Exit here" could as an End If, somehow? I removed an extraneous "Else" after fixing my original problem, and thus count three If statements and three End Ifs. However, VBA is telling me that there is a compile error now: an "End If without a block If" error on the End If shown in red. Possibly this is another derp moment, but I can't see what's wrong:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Using a Boolean function within an If statement: Argument not optional error mess

    1. I am not able to duplicate that behavior. When run a sub that calls a function which uses another function, and then step through the code using F8, I am taken to each function as expected. This happens regardless of whether the sub, function, and secondary function are in the same module or all in different modules.


    2. Its because when you use If ... Then ... on the same line, it does not need an End If. And you use that on this line:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    12-07-2011
    Location
    minneapolis, mn
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Using a Boolean function within an If statement: Argument not optional error mess

    1. Huh, ok. I'll see if I can figure out why this is happening.
    2. Ah, I see. Thank you for the explanation.

+ 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