Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

I'm trying to find a way to search for text in a cell's formula.

Is this something that could be done with a UDF ?

For example ...

SearchFormula(find_text,within_text) where find_text could be a
single value or a multi-valued named range.

If A1 contained =VLOOKUP(One,$A:$B,2,FALSE)

and C1=One, C2=Two, C3=Three

and $C$1:$C$3 was a named range called "Numbers"

The formula would look like =SEARCHFORMULA(Numbers,A1) and would
return a Boolean value of 1.

If a match wasn't found, it would return a Boolean value of 0.

Any ideas ?




- Ronald K.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Made a mistake with the desired result.

The SEARCHFORMULA result should return the value found.

In the example above, if A1's formula contained the text "One", the
result should be "One".



- Ronald K.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Creating a UDF to search a cell's formula

On Sun, 24 Jul 2011 14:45:15 -0700 (PDT), kittronald wrote:

Made a mistake with the desired result.

The SEARCHFORMULA result should return the value found.

In the example above, if A1's formula contained the text "One", the
result should be "One".



- Ronald K.


Assuming you still want to return a zero if no match, then:

=========================
Option Explicit
Function SEARCHFORMULA(find_text As Range, within_text As Range) As Variant
Dim vFindText As Variant
Dim s As String
Dim i As Long
s = within_text.Formula
vFindText = find_text
SEARCHFORMULA = 0

For i = LBound(vFindText) To UBound(vFindText)
If InStr(1, s, vFindText(i, 1), vbTextCompare) 1 Then
SEARCHFORMULA = vFindText(i, 1)
Exit For
End If
Next i
End Function
==========================
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

That works !

Thanks for the late night save.



- Ronald K.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Creating a UDF to search a cell's formula

On Sun, 24 Jul 2011 21:28:50 -0700 (PDT), kittronald wrote:

Ron,

That works !

Thanks for the late night save.



- Ronald K.


Glad to help. Thanks for the feedback.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

If you're still watching this thread, how could you use this
function within a macro ?



- Ronald K.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Creating a UDF to search a cell's formula

On Sun, 11 Sep 2011 18:00:55 -0700 (PDT), kittronald wrote:

Ron,

If you're still watching this thread, how could you use this
function within a macro ?



- Ronald K.


Just set your variable equal to the result of the formula:

v = SEARCHFORMULA(find_text As Range, within_text As Range)
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

Thanks for the quick response.

Could this function be adapted to search a formula in a defined name's
Refers to: field ?



- Ronald K.


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Creating a UDF to search a cell's formula

On Tue, 13 Sep 2011 18:33:42 -0400, "kittronald" wrote:

Ron,

Thanks for the quick response.

Could this function be adapted to search a formula in a defined name's
Refers to: field ?



- Ronald K.


You can use a similar algorithm but you would have to rewrite it as it expects ranges to be passed as argument.
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

I've been trying to get
=SEARCHFORMULA(Numbers,ActiveSheet.Names("Numbers_ Formula").RefersTo) to
work.

However, given my limited development skills, it appears this is beyond
my ability.

Perhaps, there's another way.

Thanks for getting back to me.



- Ronald K.




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

I've tried the following, but the debugger keeps displaying a Run-
time error '13': Type mismatch.


Option Explicit
Function SEARCHFORMULA(find_text As Variant, within_text As Variant)
As Variant
Dim vFindText As Variant
Dim s As Variant
Dim i As Long
s = within_text
vFindText = find_text
SEARCHFORMULA = 0


For i = LBound(vFindText) To UBound(vFindText)
If InStr(1, s, vFindText(i, 1), vbTextCompare) 1 Then
SEARCHFORMULA = vFindText(i, 1)
Exit For
End If
Next i
End Function


What is it that I'm not setting correctly ?



- Ronald K.
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Creating a UDF to search a cell's formula

On Sat, 24 Sep 2011 14:41:18 -0700 (PDT), kittronald wrote:

Ron,

I've tried the following, but the debugger keeps displaying a Run-
time error '13': Type mismatch.


Option Explicit
Function SEARCHFORMULA(find_text As Variant, within_text As Variant)
As Variant
Dim vFindText As Variant
Dim s As Variant
Dim i As Long
s = within_text
vFindText = find_text
SEARCHFORMULA = 0


For i = LBound(vFindText) To UBound(vFindText)
If InStr(1, s, vFindText(i, 1), vbTextCompare) 1 Then
SEARCHFORMULA = vFindText(i, 1)
Exit For
End If
Next i
End Function


What is it that I'm not setting correctly ?




Possibly find_text is not a range or an array. If it is not, then vFindText will not be an array, but rather just a simple variable.
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

When stepping through the code, the following line causes the error:

For i = LBound(vFindText) To UBound(vFindText)

When using SEARCHFORMULA as a UDF in a cell, Find_text is a named range
(i.e., Sheet1!$A$1:$A$5) and Within_text is the Refers to: field of a name.

The difference between using the SEARCHFORMULA UDF in a cell and in a
macro is the data type of Within_text.

The cell version uses a defined name for Within_text that contains a
text string (i.e. =CONCATENATE("SUM","PRODUCT")).

The macro version uses a name's Refers to: field.

How can I tell what data type is being expected ?




- Ronald K.


  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Creating a UDF to search a cell's formula

Ron,

When stepping through the code, the following line causes the error:

For i = LBound(vFindText) To UBound(vFindText)

When using SEARCHFORMULA as a UDF in a cell, Find_text is a named range
(i.e., Sheet1!$A$1:$A$5) and Within_text is the Refers to: field of a name.

The difference between using the SEARCHFORMULA UDF in a cell and in a
macro is the data type of Within_text.

The cell version uses a defined name for Within_text that contains a
text string (i.e. =CONCATENATE("SUM","PRODUCT")).

The macro version uses a name's Refers to: field.

How can I tell what data type is being expected ?




- Ronald K.


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
How do I use a referenced cell's formula instead of formula output? Farrelltw Excel Worksheet Functions 0 February 10th 06 05:00 PM
Creating a conditional format for a cell based on another cell's v steve-o Excel Discussion (Misc queries) 2 October 26th 05 03:51 PM
How do I use a cell's text, "A1", and put it in a formula? HighRiser Excel Worksheet Functions 1 July 6th 05 04:11 PM
Formula that will allow you to add one to cell's value by clickin. lb Excel Worksheet Functions 1 April 21st 05 06:20 PM
Setting a cell's formula with VBA [email protected] Excel Programming 3 January 12th 05 07:37 PM


All times are GMT +1. The time now is 07:30 PM.

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

About Us

"It's about Microsoft Excel"