Results 1 to 8 of 8

Thread: Passing optional parameters to Excel functions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    17

    Passing optional parameters to Excel functions

    I am trying to define my own function which would pass on given values to the Range.Find method.

    expression .Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    expression A variable that represents a Range object.

    All but the first parameter (What) are optional.

    If I have my own sub MyFind

    Code:
    Function MyFind(GivenRange as Excel.Range, _
    	 ByVal What As Object = Nothing, _
    	 Optional ByVal After As Object = Nothing, _
    	 Optional ByVal LookIn As Object = Nothing, _
    	 Optional ByVal LookAt As Object = Nothing, _
    	 Optional ByVal SearchOrder As Object = Nothing, _
    	 Optional ByVal SearchDirection As XlSearchDirection = XlSearchDirection.xlNext, _
    	 Optional ByVal MatchCase As Object = Nothing, _
    	 Optional ByVal MatchByte As Object = Nothing, _
    	 Optional ByVal SearchFormat As Object = Nothing _
    ) As Range
      ..=GivenRange.Find(What:=What, After:=After, LookIn:=LookIn, LookAt:=LookAt, SearchOrder:=SearchOrder, _
                              SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, _
                              SearchFormat:=SearchFormat)
      ..
    End Function
    I get an exception when I pass on the default Nothing values to the native Find method. However, VB needs the default values of the objects to be specified (in this case Nothing).

    How do I pass on only the non-empty parameters when my function is called? One way is to have a select case statement handling each and every combination of specified and non-specified parameters, but given the 8 optional parameters, this leads to 256 cases.

    Anyone knows the solution? Thanks.

    Shankar

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Passing optional parameters to Excel functions

    I'm assuming there's other code... otherwise what would be the point in wrapping the excel call with one of your own.

    but anyways... what's the exception you're getting?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    17

    Re: Passing optional parameters to Excel functions

    I get 'Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))'

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Passing optional parameters to Excel functions

    as some of the parameters are required for the find function, they can not really be optional unless some default value is used
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    17

    Re: Passing optional parameters to Excel functions

    For the native Find function, all parameters except the 'What' value are optional.

    For example,
    MatchCell = Range1.Find("Hello")
    MatchCell = Range1.Find("Hello", MatchCase:=True)

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Passing optional parameters to Excel functions

    all parameters except the 'What' value are optional.
    but i doubt that the default values used on omission are nothing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    17

    Re: Passing optional parameters to Excel functions

    They default to whatever they were the last time the function was called. The question is, how do I selectively pass on only the parameters passed to my wrapper function?

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Passing optional parameters to Excel functions

    you can try not assigning any default values to your optional parameters, else only pass parameters that have assigned values passed to the function, by using empty variabels or buidling the find parameter string within the function
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width