Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default FIND method doesn't work in UDF. What's wrong with this code?

Windows XP - Excel 2000
Hello,
I'm trying to make a function that returns the row number of the cell
in range SAR where string CRIT appears for the first time.
The purpose is to use the result as an argument in an INDEX function to
retrieve the value of the intersection of a certain row and column.

The INDEX(MATCH,MATCH) formula is useless in this case since the
horizontal criteria are not on the same row.

What is wrong with the following code ? I always get the #VALUE!
result. (I know about line breaking underscores missing in this
message. They are OK in the module.)

Function GetRowNumber(SAR As Range, CRIT As String) As Long
GetRowNumber = SAR.Find(What:=CRIT, After:=SAR.Range("A1"),
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False).Row
End Function

THE FOLLOWING SUB-VERSION WORKS FINE THOUGH.
Sub RowNum()
Dim SAR As Range, CRIT As String
Set SAR = Application.InputBox("Select Search Area", Type:=8)
CRIT = Application.InputBox("Enter Search Value", Type:=2)
MsgBox SAR.Find(What:=CRIT, After:=SAR.Range("A1"), LookIn:=xlFormulas,
_
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False).Row
End Sub

Can anyone help me out here?
Thank you very much
Herman

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default FIND method doesn't work in UDF. What's wrong with this code?

Hi Herman,

Find indeed doesn't work in a UDF. This is probably because there is a
replace option and changing worksheets from a UDF is prohibited.
Subs however do have permission to change worksheets, so Find does work
there.

--
Kind regards,

Niek Otten

Microsoft MVP - Excel

wrote in message
oups.com...
Windows XP - Excel 2000
Hello,
I'm trying to make a function that returns the row number of the cell
in range SAR where string CRIT appears for the first time.
The purpose is to use the result as an argument in an INDEX function to
retrieve the value of the intersection of a certain row and column.

The INDEX(MATCH,MATCH) formula is useless in this case since the
horizontal criteria are not on the same row.

What is wrong with the following code ? I always get the #VALUE!
result. (I know about line breaking underscores missing in this
message. They are OK in the module.)

Function GetRowNumber(SAR As Range, CRIT As String) As Long
GetRowNumber = SAR.Find(What:=CRIT, After:=SAR.Range("A1"),
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False).Row
End Function

THE FOLLOWING SUB-VERSION WORKS FINE THOUGH.
Sub RowNum()
Dim SAR As Range, CRIT As String
Set SAR = Application.InputBox("Select Search Area", Type:=8)
CRIT = Application.InputBox("Enter Search Value", Type:=2)
MsgBox SAR.Find(What:=CRIT, After:=SAR.Range("A1"), LookIn:=xlFormulas,
_
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False).Row
End Sub

Can anyone help me out here?
Thank you very much
Herman



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default FIND method doesn't work in UDF. What's wrong with this code?

Hi Niek,

Find DOES work in UDF's. An example which works fine for me:
----- snip here --------
Public Function holidays(ByVal s_ccy As String) As Variant
'Returns array of holidays for a given currency

Dim rng_ccy As Range

With Sheets("Holidays")
Set rng_ccy = .Range("A2:IV2").Find(s_ccy, LookIn:=xlValues,
LookAt:=xlWhole) 'Search for currency in row 2
If rng_ccy Is Nothing Then
holidays = CVErr(xlErrValue) 'Not found: return value error
Else
holidays = .Cells(5, rng_ccy.Column).Resize(.Cells(3,
rng_ccy.Column), 1) 'Row 3 consists of number of holidays
End If
End With

End Function
------- snip again -----------

Herman, I suggest to debug your code in detail step by step...

Regards,
Bernd
--
Use non-volatile INDEX(P11:IV65536,1+w,1+y):INDEX(P11:IV65536,w+y,x +z)
instead of volatile OFFSET(P11,w,x,y,z).

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default FIND method doesn't work in UDF. What's wrong with this code?

Thanks Niek.
You taught me something important.
Do you have any suggestions on how to get that row number, either with
a UDF or a WS function ?
Herman

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default FIND method doesn't work in UDF. What's wrong with this code?

Hallo Bernd,
Thanks for your help.
But how can I debug a code with only one line ?
And by the way, what do you mean by
"Use non-volatile
INDEX(P11:IV65536,1+w,1+y):IND*EX(P11:IV65536,w+y, x+z)
instead of volatile OFFSET(P11,w,x,y,z). "

Kind Regards,
Herman



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default FIND method doesn't work in UDF. What's wrong with this code?

Hi Herman,

You can set a breakpoint at that line (left mouse-click in the editor to the
left of that line, one or more bordeaux-red dots will appear) and you can add
watches for interesting values (for example for SAR and for CRIT: right
mouse-click at CRIT and again at SAR and add a watch for them). Then start
the calculation. The macro will be interrupted at the breakpoint and you can
see the watched values at this state.

By the way: Your macro has no problem in my example (A1:A8 filled with
a,b,c,...,h; cell C4 filled with h, C5 = getrownumber(A1:A8,C4) returns 8
which is ok.

My statement below "--" just shows my general opinion not to use the OFFSET
worksheet function. Does not apply to your problem.

Regards,
Bernd
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default FIND method doesn't work in UDF. What's wrong with this code?

Find started working in xl2002. Before that, it didn't work in UDFs.

Are you using xl2002+?

Bernd Plumhoff wrote:

Hi Niek,

Find DOES work in UDF's. An example which works fine for me:
----- snip here --------
Public Function holidays(ByVal s_ccy As String) As Variant
'Returns array of holidays for a given currency

Dim rng_ccy As Range

With Sheets("Holidays")
Set rng_ccy = .Range("A2:IV2").Find(s_ccy, LookIn:=xlValues,
LookAt:=xlWhole) 'Search for currency in row 2
If rng_ccy Is Nothing Then
holidays = CVErr(xlErrValue) 'Not found: return value error
Else
holidays = .Cells(5, rng_ccy.Column).Resize(.Cells(3,
rng_ccy.Column), 1) 'Row 3 consists of number of holidays
End If
End With

End Function
------- snip again -----------

Herman, I suggest to debug your code in detail step by step...

Regards,
Bernd
--
Use non-volatile INDEX(P11:IV65536,1+w,1+y):INDEX(P11:IV65536,w+y,x +z)
instead of volatile OFFSET(P11,w,x,y,z).


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default FIND method doesn't work in UDF. What's wrong with this code?

Hi Dave,

Yes, my version is Excel 2002.

Regards,
Bernd
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default FIND method doesn't work in UDF. What's wrong with this code?

Hi Niek,

Sorry - wrong version, as Dave pointed out.

Regards,
Bernd
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default FIND method doesn't work in UDF. What's wrong with this code?

Thanks, Dave, I didn't know that!

--
Kind regards,

Niek Otten

Microsoft MVP - Excel

"Dave Peterson" wrote in message
...
Find started working in xl2002. Before that, it didn't work in UDFs.

Are you using xl2002+?

Bernd Plumhoff wrote:

Hi Niek,

Find DOES work in UDF's. An example which works fine for me:
----- snip here --------
Public Function holidays(ByVal s_ccy As String) As Variant
'Returns array of holidays for a given currency

Dim rng_ccy As Range

With Sheets("Holidays")
Set rng_ccy = .Range("A2:IV2").Find(s_ccy, LookIn:=xlValues,
LookAt:=xlWhole) 'Search for currency in row 2
If rng_ccy Is Nothing Then
holidays = CVErr(xlErrValue) 'Not found: return value error
Else
holidays = .Cells(5, rng_ccy.Column).Resize(.Cells(3,
rng_ccy.Column), 1) 'Row 3 consists of number of holidays
End If
End With

End Function
------- snip again -----------

Herman, I suggest to debug your code in detail step by step...

Regards,
Bernd
--
Use non-volatile INDEX(P11:IV65536,1+w,1+y):INDEX(P11:IV65536,w+y,x +z)
instead of volatile OFFSET(P11,w,x,y,z).


--

Dave Peterson





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
BreakLinks method doesn't work. Gop Excel Discussion (Misc queries) 0 September 1st 05 12:27 PM
How Come Calcs are wrong when verifing totals to items method beejee Excel Worksheet Functions 2 May 11th 05 05:49 AM
Using variables to make a date and using find method to find that. KyWilde Excel Programming 2 April 21st 05 09:43 PM
Find Method doesn't work Slugo Excel Programming 2 November 18th 03 03:09 PM
Cant get my code work. Find file or create it Poseilus Excel Programming 1 October 12th 03 03:42 PM


All times are GMT +1. The time now is 12:23 AM.

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"