View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Harlan Grove
 
Posts: n/a
Default Use InStr function in formula?

Tushar Mehta wrote...
If you are like me you would probably go with a pass-through VBA function.

function VBAInStrRev({all the arguments to InstrRev)
VBAInStrRev = InStrRev({all the arguments to InstrRev)
end function

We'll soon hear the howls of all those who prize saving 3.141592 CPU cycles
over the loss of transparency, maintainability, and ease of understanding.
Since I am not one of them, yes, I would strongly consider use of the pass-
through function.


It's more than a few CPU cycles. The Excel/VBA udf interface isn't
quick. Also, unless there's documentation that goes along with that
udf, there's theoretical transparency and maintainability only for the
OP.

But there are other considerations. UDFs are considered macros in the
context of macro security. So the macros would need to be certified in
order to run on other PCs if they'd be used in business environments in
which macro security is usually set to high. And there'll soon be the
added complication that Excel 12 won't support such UDFs in web
services. Maybe not a concern for the OP, but legacy VBA udfs will soon
become a big headache for Excel developers. Nasty, long formulas using
only built-in functions, on the other hand, would still work even in
web services. Portability not a concern for you?

There's also the point of using the best tool for the task. In this
case, an argument could be made for using Laurent Longre's MOREFUNC.XLL
add-in. Finding the last/rightmost instance of a substring (ss) in a
given string (s) could be acomplished using regular expressions.

=REGEX.FIND(s,ss&"(?!.*"&ss&".*)")

Why arguably better? Consider finding the last instance of 'the' in

Now is the time for all good men to come to the aid of their country.

As a simple substring, it'd be the 1st 3 chars of the word 'their', but
if what's actually wanted is the *word* 'the'? It's relatively trivial:
add '\b' to both ends of ss: '\bthe\b'. Finding the last instance of a
whole word would be no trivial exercise in VBA using InStrRev or not.
This also adds the advantages that MOREFUNC.XLL's functions *are*
documented, and as an XLL add-in, it doesn't trigger macro security.

In an ideal world, Excel's FIND and SEARCH would take negative 3rd
arguments, which would mean search from right to left from the given
position (the absolute value of the 3rd argument), but Microsoft
doesn't seem to want to bother to improve or extend existing text
functions.