Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Calling an Excel function from a VBA macro

I'm having trouble writing an Excel macro (actually a function) that
will call another Excel function.

Background:

The FIND function in Excel returns the location in one string with the
first occurrence of another string. For example:
FIND("ar","marker marker") returns the number 2.

I'm trying to write a user-defined function in Excel that will find
the location of the last character of the last occurence; in other
words, starting from the right side of a particular string. My
function FINDR("ar","marker marker") would return the value 9.

There are probably several ways to do it, but here's what I have done
so far: First, I wrote a function ReverseString, which reverses all
the characters in the given string. (at the bottom of this posting;
improvements are welcome)

My idea was to use this new function to reverse both the "within_text"
string and the "find_text" strings, and then use the regular Excel
function FIND to search for the reversed "find_text" in the
"within_text" string.

In my example, the Excel FIND function would find the first "ra" in
"rekram rekram", and return the number 4. Then I would then subtract
4 from the length of the original string (13) to get the desired 9.

The problem is, when I call FIND from a VBA function, it does not do
the same thing as the FIND function. Instead, the VBA FIND does
something completely different. It's complicated and I don't really
understand it. Basically its search domain is not limited to a single
string of characters, but instead it "finds" information about the
entire worksheet. This includes fonts, other formatting information,
and other data far beyond the scope that I need.

I know I could recreate the Excel FIND in VBA, but it seems like
re-inventing the wheel.

How can I call the Excel function "FIND" from a function or macro in
Excel VBA?

The function ReverseString below works as desired. The function FINDR
does not.

Thanks for any help.

Steven

' = = = = = = = = =
Function ReverseString(in_text As String) As String
r_s = ""
For i = Len(in_text) To 1 Step -1
r_s = r_s & Mid(in_text, i, 1)
Next
ReverseString = r_s
End Function

Function FINDR(find_text As String, within_text As String) As Long
find_rev = ReverseString(find_text)
within_rev = ReverseString(within_text)

' The following line does NOT WORK
loc = Find(find_rev, within_rev)

FINDR = LEN(within_text) - loc

End Function

' = = = = = = = = =

--
Steven M - lid
(remove wax and invalid to reply)

On a web site of tips on how to use Microsoft Word:
"Word rarely misses an opportunity to perplex"
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Calling an Excel function from a VBA macro

Steven,

The inStr and RevInstr functions do what you want.
Take a look in VBA help.

Also to use an Excel worksheet functions with VBA,
prefix the call with Application or WorksheetFunction.
All 3 of these should work...
Application.Find
WorksheetFunction.Find
Application.WorksheetFunction.Find

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"Steven M (remove wax and invalid to reply)"
wrote in message
I'm having trouble writing an Excel macro (actually a function) that
will call another Excel function.
Background:
The FIND function in Excel returns the location in one string with the
first occurrence of another string. For example:
FIND("ar","marker marker") returns the number 2.

I'm trying to write a user-defined function in Excel that will find
the location of the last character of the last occurence; in other
words, starting from the right side of a particular string. My
function FINDR("ar","marker marker") would return the value 9.

There are probably several ways to do it, but here's what I have done
so far: First, I wrote a function ReverseString, which reverses all
the characters in the given string. (at the bottom of this posting;
improvements are welcome)

My idea was to use this new function to reverse both the "within_text"
string and the "find_text" strings, and then use the regular Excel
function FIND to search for the reversed "find_text" in the
"within_text" string.

In my example, the Excel FIND function would find the first "ra" in
"rekram rekram", and return the number 4. Then I would then subtract
4 from the length of the original string (13) to get the desired 9.

The problem is, when I call FIND from a VBA function, it does not do
the same thing as the FIND function. Instead, the VBA FIND does
something completely different. It's complicated and I don't really
understand it. Basically its search domain is not limited to a single
string of characters, but instead it "finds" information about the
entire worksheet. This includes fonts, other formatting information,
and other data far beyond the scope that I need.

I know I could recreate the Excel FIND in VBA, but it seems like
re-inventing the wheel.

How can I call the Excel function "FIND" from a function or macro in
Excel VBA?

The function ReverseString below works as desired. The function FINDR
does not.

Thanks for any help.

Steven

' = = = = = = = = =
Function ReverseString(in_text As String) As String
r_s = ""
For i = Len(in_text) To 1 Step -1
r_s = r_s & Mid(in_text, i, 1)
Next
ReverseString = r_s
End Function

Function FINDR(find_text As String, within_text As String) As Long
find_rev = ReverseString(find_text)
within_rev = ReverseString(within_text)

' The following line does NOT WORK
loc = Find(find_rev, within_rev)

FINDR = LEN(within_text) - loc

End Function
' = = = = = = = = =
Steven M - lid
(remove wax and invalid to reply)
On a web site of tips on how to use Microsoft Word:
"Word rarely misses an opportunity to perplex"
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Calling an Excel function from a VBA macro

Steven - try looking for InstrRev function.
Jim had right function ... but typed in the name, uh, backwards.
Ironic, no?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default Calling an Excel function from a VBA macro

Hi

you made your own ReverseString function, but VBA has already
string reverse function - that is StrReverse(expression).
if you use this function, your FINDR will be more short and simple.

Function findr(ByVal s As String, ByVal src As String) As Long
findr = Len(src) - Application.Find(StrReverse(s), StrReverse(src))
End Function

keizi

"Steven M (remove wax and invalid to reply)" wrote
in message ...
I'm having trouble writing an Excel macro (actually a function) that
will call another Excel function.

Background:

The FIND function in Excel returns the location in one string with the
first occurrence of another string. For example:
FIND("ar","marker marker") returns the number 2.

I'm trying to write a user-defined function in Excel that will find
the location of the last character of the last occurence; in other
words, starting from the right side of a particular string. My
function FINDR("ar","marker marker") would return the value 9.

There are probably several ways to do it, but here's what I have done
so far: First, I wrote a function ReverseString, which reverses all
the characters in the given string. (at the bottom of this posting;
improvements are welcome)

My idea was to use this new function to reverse both the "within_text"
string and the "find_text" strings, and then use the regular Excel
function FIND to search for the reversed "find_text" in the
"within_text" string.

In my example, the Excel FIND function would find the first "ra" in
"rekram rekram", and return the number 4. Then I would then subtract
4 from the length of the original string (13) to get the desired 9.

The problem is, when I call FIND from a VBA function, it does not do
the same thing as the FIND function. Instead, the VBA FIND does
something completely different. It's complicated and I don't really
understand it. Basically its search domain is not limited to a single
string of characters, but instead it "finds" information about the
entire worksheet. This includes fonts, other formatting information,
and other data far beyond the scope that I need.

I know I could recreate the Excel FIND in VBA, but it seems like
re-inventing the wheel.

How can I call the Excel function "FIND" from a function or macro in
Excel VBA?

The function ReverseString below works as desired. The function FINDR
does not.

Thanks for any help.

Steven

' = = = = = = = = =
Function ReverseString(in_text As String) As String
r_s = ""
For i = Len(in_text) To 1 Step -1
r_s = r_s & Mid(in_text, i, 1)
Next
ReverseString = r_s
End Function

Function FINDR(find_text As String, within_text As String) As Long
find_rev = ReverseString(find_text)
within_rev = ReverseString(within_text)

' The following line does NOT WORK
loc = Find(find_rev, within_rev)

FINDR = LEN(within_text) - loc

End Function

' = = = = = = = = =

--
Steven M - lid
(remove wax and invalid to reply)

On a web site of tips on how to use Microsoft Word:
"Word rarely misses an opportunity to perplex"


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling worksheet function from a macro Joe Farruggio Excel Worksheet Functions 3 November 20th 06 10:01 PM
calling a new function Excel gives me #NAME? Mark Dvorkin Excel Worksheet Functions 4 July 10th 05 01:29 AM
Calling of simple function in Excel 97 TomCee Excel Programming 3 April 20th 04 02:44 PM
Calling up the Solver function in a VBA macro Ken Wright Excel Programming 3 January 28th 04 04:21 PM
Calling a function from within EXCEL pcor[_2_] Excel Programming 3 July 18th 03 05:10 PM


All times are GMT +1. The time now is 12:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"