+ Reply to Thread
Results 1 to 6 of 6

Adding Second Variable to a Function

  1. #1
    Registered User
    Join Date
    08-16-2007
    Posts
    50

    Adding Second Variable to a Function

    Hello All,
    I'm writing a function that I can pass a name of a table and column name and it will sort the table by the desired column. I'm testing it in pieces as I go. I started by recording a macro that sorts. To test my syntax for writing a function with variables, I took the code from my recording an put it in a function, then added variables that don't do anything yet. It works fine if I only include one variable to be passed to the function, but as soon as I add a second variable to be passed to the function I get an error in the sub I'm using to test the function. The error says "Expected =". Does anyone know why this is happening?
    Thanks.

    Here is my code:
    Please Login or Register  to view this content.

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Adding Second Variable to a Function

    I think it needs an = here:
    Please Login or Register  to view this content.
    But it wont let you have test and test2, plus it gives other errors (at least on my sheet)
    Last edited by gmr4evr1; 02-08-2015 at 04:59 PM.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    08-16-2007
    Posts
    50

    Re: Adding Second Variable to a Function

    I tried a simpler example and got the same result. It still says "expected =". Does a function have to return something? Is there a limit on the number of variables that a function can have passed to it? Below are examples of my samples that work with a single variable and don't work with two variables.

    This code works:
    Please Login or Register  to view this content.

    This code does not work:
    Please Login or Register  to view this content.
    Thank you

  4. #4
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Adding Second Variable to a Function

    I get a Compile Error: Syntax Error when I try to run the second code. Its got me baffled.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Adding Second Variable to a Function

    Hi,

    You have (re)discovered the joys of Excel VBA syntax.

    I dont' know the reason (and don't really care), but your syntax is apparently NOT allowed for functions with more than one formal parameter. IMHO you did nothing wrong, except the syntax won't work.

    You seem to need either:
    a. Call FunctionName, or
    b. FunctionName arg1, arg2, etc.


    Try the following tested and working code:
    Please Login or Register  to view this content.
    Lewis

  6. #6
    Registered User
    Join Date
    08-16-2007
    Posts
    50

    Re: Adding Second Variable to a Function

    Thanks! That worked.

+ 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. Replies: 2
    Last Post: 08-22-2014, 03:55 AM
  2. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  3. vlookup variable filepath one file variable can this be done via a function?
    By pauldbaker in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-15-2013, 10:10 AM
  4. Adding a variable to index & match function using VBA
    By prestopr in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-07-2013, 05:48 PM
  5. Adding variable to specific sheetname in a Sumif function
    By captaincrunch in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 02-04-2010, 12:44 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