+ Reply to Thread
Results 1 to 10 of 10

.Precedents don't work in UDF

  1. #1
    Registered User
    Join Date
    06-30-2006
    Posts
    5

    .Precedents don't work in UDF

    I need to use the precedents of the cell that is the argument of a user-defined function as well as the cell itself. But if I write
    Function MyFormula (MyCell as Range)
    MyCell.Precedents don't work in this function. What can be done with it?

  2. #2
    Registered User
    Join Date
    10-28-2003
    Posts
    21
    I'm not even really sure what precedents are, but you could try

    Function MyFormula (MyCell as object)

    instead...

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    An example


    Function MyFormula(ByRef MyCell As Range)
    Dim rng As Range
    Dim str As String

    For Each rng In MyCell.Precedents
    str = str & rng.Address & " , "
    Next
    MyFormula = str
    End Function

  4. #4
    Registered User
    Join Date
    06-30-2006
    Posts
    5

    Unhappy

    Quote Originally Posted by tony h
    An example


    Function MyFormula(ByRef MyCell As Range)
    Dim rng As Range
    Dim str As String

    For Each rng In MyCell.Precedents
    str = str & rng.Address & " , "
    Next
    MyFormula = str
    End Function
    Perhaps something is wrong with my Excel, but the fact is that this code doesn't work It returns only the address of MyCell (God knows why! )

  5. #5
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    I ran the code a s a sub which was ok then changed it to a formula which wasn't.

    Sub a()
    Dim rng As Range
    Dim str As String
    Dim mycell As Range

    Set mycell = Selection

    str = "direct : "
    For Each rng In mycell.DirectPrecedents
    str = str & rng.Address & " , "
    Next


    str = str & " indirect : "
    For Each rng In mycell.Precedents
    str = str & rng.Address & " , "
    Next
    Debug.Print str

    End Sub
    should work all right as a sub. Need to think about why it failed as a formula. Maybe it needs the cell selected?

    Sorry need to go

  6. #6
    Registered User
    Join Date
    06-30-2006
    Posts
    5
    I also tried to create Sub()-procedure when I detected the problem The same result.
    I explored the I-net - think here is the explanation of the problem: http://support.microsoft.com/?scid=k...=1759&sid=1289
    But how can I solve it in this particular situation? Perhaps somebody can give a piece of advice?

  7. #7
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    I don't thik that is the answer. The range is being passed by reference and nothing is being changed on the worksheet. A value is being passed back but that is valid.

    regards,
    interesting. Might have a chance to play about with this on Tuesday if it has not been resolved.

  8. #8
    Registered User
    Join Date
    06-30-2006
    Posts
    5
    But in a certain sense each precedent is a range as well. And it is not passed to the function as an argument.

  9. #9
    lexcel
    Guest

    Re: .Precedents don't work in UDF

    Hi Andruha,

    The only thing what I can think of is to capture the change of any of
    the changed cells with the Worksheet_Change event subroutine. Use
    Tony's code on the "Target" parameter to find the precedents and just
    insert them in the sheet where you want them.
    Not very elegant, but it should work.

    If you can't figure out how, let me know and I'll spend some time
    coding it.

    Regards,

    Lex

    > But how can I solve it in this particular situation? Perhaps somebody
    > can give a piece of advice?



  10. #10
    Registered User
    Join Date
    06-30-2006
    Posts
    5
    Unfortunatelly, but I also think it's the only possible way. Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1