|
-
May 3rd, 2013, 09:42 AM
#1
Thread Starter
Junior Member
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
-
May 3rd, 2013, 10:56 AM
#2
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
-
May 3rd, 2013, 11:53 AM
#3
Thread Starter
Junior Member
Re: Passing optional parameters to Excel functions
I get 'Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))'
-
May 3rd, 2013, 07:00 PM
#4
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
-
May 3rd, 2013, 11:11 PM
#5
Thread Starter
Junior Member
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)
-
May 4th, 2013, 03:52 AM
#6
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
-
May 4th, 2013, 04:02 AM
#7
Thread Starter
Junior Member
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?
-
May 4th, 2013, 04:07 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|