VBA: REDIM Function?

Jonnyabc

New Member
Joined
Jan 25, 2011
Messages
6
Someone...I know someone...is bound to say it's bad practice to make a function declared as a particular variable type should remain that same variable type and is bad coding to do otherwise. However, I am curious if there is a way to do so...

The scenario is this: I created a RegEx function, but sometimes I want it to return Boolean TRUE/FALSE for if() statements and filters, other times, I may want it to actually return the value (if it is) found.

Thus far, the first part has been possible, but now the stumbling block is after I do:

Code:
objRegEx.Test(strValue)

If this criteria is TRUE, and my [optional] argument is also set to return the #x result, it will return the string.

I'm not necessarily saying I'm going that route, but I am curious if it is at least possible with REDIM or the like and how?


Thanks!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
The reason can be in declaring of the passed variable As String
See the difference in passing of variant variable X and string variable S to the function:

Rich (BB code):

Sub Test()
  
  Dim S As String, x, ret
  
  ' It's Ok, returs boolean
  ret = MyRegEx("regex", x)
  Debug.Print "ret", ret, TypeName(ret)
  Debug.Print "x", x, TypeName(x)
  
  ' S is alwaus string because S is declared as string
  ret = MyRegEx("regex", S)
  Debug.Print "ret", ret, TypeName(ret)
  Debug.Print "S", S, TypeName(S)
  
End Sub

' Do nothing, just returns boolean - for debug only
Function MyRegEx(strValue As String, Optional ret) ' As Boolean
  With New RegExp
    ret = .Test(strValue)
  End With
  MyRegEx = ret
End Function
 
Last edited:
Upvote 0
For string to boolean conversion use CBool("StringValue"):
Debug.Print TypeName(CBool("TRUE"))
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top