+ Reply to Thread
Results 1 to 4 of 4

optional arguments type

  1. #1
    Registered User
    Join Date
    11-20-2003
    Location
    UK
    Posts
    18

    optional arguments type

    Hi - 'VBA Made Easy' says that optional arguments for both Functions and Subroutines should not be given a type as they are Variants. eg

    Function Test (optional Counter) and not Function Test (optional Counter as integer).

    I am using Excel 97, not specifying type and am getting 'type mismatch' errors.
    What are the rules please.
    TKIA

  2. #2
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Hi

    You get the Type Mismatch error because if not specified the argument returns 'Missing'. This only happens with Variant arguments. Your book is incorrect - you can specify the type of arguments. One reason why you may not want to do so is to specifically test whether the function has been called with that argument (in which case the argument must be a variant allowing you to use the IsMissing VBA function on the argument which returns True if argument omitted or False if not).

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    optional arguments for both Functions and Subroutines should not be given a type as they are Variants
    That's an amazingly misinformed statement for a book.

    There are a few reasons to use Variants. One is if the data may be of varying type (string, number, boolean) and your code needs to know which. Another is if you specifically need to know whether the argument was passed using IsMissing().

    Otherwise, for performance and practice, you should assuredly specify the data type. You can specify defaults:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-20-2003
    Location
    UK
    Posts
    18

    Smile

    Hi - thanks for that. I will go through and put the types in using the shg method and also remove all my ismissing statements.

+ 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. sort error
    By dlh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-11-2008, 05:42 PM
  2. Loading Custom Data Type ?
    By dpenny in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-01-2007, 06:19 PM
  3. Hyperlink to a file regardless of file type
    By djmarak in forum Excel General
    Replies: 2
    Last Post: 09-21-2007, 12:24 PM
  4. format Cells type "text"
    By matrixacc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2007, 04:22 AM
  5. For type loop verse Do type loop
    By boylejob in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2007, 03:02 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