Results 1 to 20 of 20

Thread: [RESOLVED] My Public Function only works in a worksheet cel. Versions conflict!

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    Resolved [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:
    1. Public Function ValueOneBack() As Variant
    2.    
    3.     ValueOneBack = ""
    4.    
    5.     Application.Volatile
    6.    
    7.     If TypeName(Application.Caller) = "Range" Then
    8.         If Application.Caller.Row <> 65536 And Application.Caller.Column <> 1 Then
    9.             ValueOneBack = Application.Caller.Offset(1, -1).Value
    10.         End If
    11.     End If
    12.  
    13. End Function
    14.  
    15. Function MatchOneBack() As Integer
    16.     Dim LookupValue As Variant
    17.     Dim rngMatrix As Range
    18.    
    19.     LookupValue = ValueOneBack
    20.    
    21.     Set rngMatrix = Worksheets(1).Range("B1:B4")
    22.        
    23.     If rngMatrix.Find(What:=LookupValue, _
    24.                         LookIn:=xlValues, _
    25.                         LookAt:=xlWhole, _
    26.                         MatchCase:=False) Is Nothing Then
    27.         MatchOneBack = 0
    28.     Else
    29.         MatchOneBack = 2
    30.     End If
    31.        
    32.     Set rngMatrix = Nothing
    33.    
    34. End Function
    Last edited by Tobian; Feb 22nd, 2006 at 10:23 AM.

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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"?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    Re: My Public Function only works when inserted in a worksheet cel

    Thanks, I try. I can have some help with debugging statements. What do you mean with that?

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: My Public Function only works when inserted in a worksheet cel

    In your function "MatchOneBack":
    Code:
            ...
        LookupValue = ValueOneBack
        'Add Here:
        MsgBox LookupValue.Address
            ...
    This will pop up a message box with the Address of the Range Object that is returned by the call to the ValueOneBack function.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    Re: My Public Function only works when inserted in a worksheet cel

    I tried MsgBox LookupValue.Address and get Runtime error 424, Object needed.
    What could this mean?

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    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.

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: My Public Function only works when inserted in a worksheet cel

    I inserted a new "B" Column. Here are two sets of results:
    "ValueOneBack" is now in Cell D2
    "MatchOneBack" is now in Cells D3 and E3
    Code:
        A      B       C       D       E
    1   0	0	I'm B1	I'm C1	I'm D1
    2   0	I'm A4	I'm B2	I'm B3	I'm D2
    3   0	I'm D4	I'm B3	0	0
    4   0	0	I'm B4	I'm C4	I'm D4
    
        A      B       C       D       E
    1   0	0	I'm B1	I'm C1	I'm D1
    2   0	I'm B4	I'm B2	I'm B3	I'm D2
    3   0	I'm C4	I'm B3	2	2
    4   0	0	I'm B4	I'm C4	I'm D4
    It looks like it works fine to me???
    Last edited by Webtest; Feb 20th, 2006 at 01:23 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    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.

  10. #10
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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".
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  11. #11
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: My Public Function only works when inserted in a worksheet cel

    Here are the formulas I am using in my cells:
    Code:
    In Cell D2:    =ValueOneBack()
    In Cell D3:    =matchoneback()
    In Cell E3:    =matchoneback()
    I'm using Excel 2003, but I could try it in '97 if I have the time.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    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.

  13. #13
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  14. #14
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: My Public Function only works when inserted in a worksheet cel

    It appears to be broken in '97 ... I'll try to figure out why.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  15. #15

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    Re: My Public Function only works when inserted in a worksheet cel

    I zipped my worksheet with macro, try if it works in your version, please
    Attached Files Attached Files

  16. #16
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  17. #17
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: My Public Function only works when inserted in a worksheet cel

    This works in '97 in place of your "Find" function.
    Code:
        Dim aCell As Range
        For Each aCell In rngMatrix
            If aCell.Value = LookupValue Then
                MatchOneBack = 2
                Exit For
            Else
                MatchOneBack = 0
            End If
        Next aCell
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  18. #18
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: My Public Function only works when inserted in a worksheet cel

    Of course you can shorten the search function:
    Code:
        Dim aCell As Range
        MatchOneBack = 0
        For Each aCell In rngMatrix
            If aCell.Value = LookupValue Then
                MatchOneBack = 2
                Exit For
            End If
        Next aCell
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  19. #19

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Holland
    Posts
    34

    Re: My Public Function only works when inserted in a worksheet cel

    Thanks, problem is solved. Good to know that Office versions can react differently on same code.

  20. #20
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [RESOLVED]My Public Function only works when inserted in a worksheet cel

    The lousy part is when Worksheet Functions don't work AND they don't generate any warnings! It's a wonderful life, but nobody said it's easy!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width