[RESOLVED] My Public Function only works in a worksheet cel. Versions conflict!
When I insert following Public Function ValueOneBack() in a Cell of a Excel worksheet, it works.
Eg. when I insert ValueOneBack() in Cell H7, it returns the value of Cell G8.
Now I want to know if the result of ValueOneBack() is also present in Range B1:B4. So I use the result of ValueOneBack() as LookupValue in the Function MatchOneBack(). But now ValueOneBack() doesn't return a value, which makes MatchOneBack() always false.
Can somebody tell me what I do wrong?
VB Code:
Public Function ValueOneBack() As Variant
ValueOneBack = ""
Application.Volatile
If TypeName(Application.Caller) = "Range" Then
If Application.Caller.Row <> 65536 And Application.Caller.Column <> 1 Then
Re: My Public Function only works when inserted in a worksheet cel
I'm really shaky on this one, but my guess is that the problem is in your Find statement:
If rngMatrix.Find(What:=LookupValue, _
I think "What" expects a string(?), and you are giving it a Range Object. Since Function "ValueOneBack" only returns a Range Object, why don't you just declare it as returning Type Range?
Again, I really don't know much about this ... but have you put in debugging statements, such as "MsgBox LookupValue.address" right after you set it in "MatchOneBack"?
Re: My Public Function only works when inserted in a worksheet cel
About
my guess is that the problem is in your Find statement:
If rngMatrix.Find(What:=LookupValue, _
I think "What" expects a string(?), and you are giving it a Range Object. Since Function "ValueOneBack" only returns a Range Object, why don't you just declare it as returning Type Range?
When I put a value in Range B1:B4 , eg. "3" in Cell B3 and I give LookupValue = 3 in MatchOneBack() in stead of ValueOneBack, it works. How do I have to declare LookupValue? Don't know how to declare the returning Type Range.
Re: My Public Function only works when inserted in a worksheet cel
I sent you a private message ... I apologize, your Function "ValueOneBack" DOES return a value ... not a Range Object. Just change the message box call to "MsgBox LookupValue". I generated the following matrix and put "=ValueOneBack()" in Cell C2. You'll notice that it does update with the value in Cell B3. I put "=MatchOneBack" in Cell C3 and Cell D3. The result in Cell C2 is "2" indicating that the string "I'm B4" IS matched in B1:B4. The result in Cell D3 is "0" indicating that the string "I'm C4" was NOT matched in B1:B4. This indicates to me that the "Find" is working as expected.
Code:
A B C D
1 0 I'm B1 I'm C1 I'm D1
2 0 I'm B2 I'm XX I'm D2
3 0 I'm XX 2 0
4 0 I'm B4 I'm C4 I'm D4
I'm going to add one more column to my matrix and test it, but so far, it seems to work as coded. What is different from what you are trying to do?
Re: My Public Function only works when inserted in a worksheet cel
no problem, Glad you help me. I tried your code but get "0" in C3..!? It is different from your outcome, how come? What version your working with? My version is 2000.
Last edited by Tobian; Feb 20th, 2006 at 01:31 PM.
Re: My Public Function only works when inserted in a worksheet cel
What did you get in the popup Message Box?
Select (highlight) your matrix and copy it into NotePad. Then copy the NotePad into your next post, enclosing it in code tags. Of course, I manually added the Column legends across the top and the Row Legends along the left edge. You don't need to do that.
I am using your exact code that you posted, with the addiition of the MsgBox call to display "LookupValue".
Re: My Public Function only works when inserted in a worksheet cel
I got a lot of msg boxes I had to click away. But it was containing "I'm B4", that's ok. But it is strange that it works in your version and not in mine.
Re: My Public Function only works when inserted in a worksheet cel
Yes, the message boxes will pop up for every instance of "MatchOneBack" every time an update is done. Does the string "I'm B4" exist in B1:B4? If so, you've got to be getting a "2" in Cell C3 ??? Let me try it in '97.
Re: My Public Function only works when inserted in a worksheet cel
Duhhh ... Monday Mornings. "Find" is a WORKSHEET function somewhere prior to version 2003. You have to use "Application.WorksheetFunction.", HOWEVER, not all worksheet functions can be called from VBA!!! I couldn't get it to work with '97. You'll probably have to use a 'native' VBA search function.
Sorry, I can't unzip anything in my present incarnation.