Consulting

Results 1 to 9 of 9

Thread: Adding Comments to a Formula

  1. #1

    Adding Comments to a Formula

    I just saw an interesting tip for adding comments to the end of a formula. In VBA when I want to add a comment to a statement line, I just preface the comment with an apostrophe ( ) like:

    Range(“A1”).Select ‘This is a comment

    To do something similar in a formula, the author says to add a sequence like:

    +N(“Comment”)

    The comment will be displayed in the formula line at the top of the screen, but will not be seen in the cell where the formula is written. Klutsy, but perhaps useful for documentation sometimes.

    Note that the “N” is the “Value” function as used here. It attempts to convert the text comment into a number, but fails without generating an error message. Actually N generates a zero when it fails. It will always fail when trying to convert a text value.

    Will this technique cause any kind of problem?

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Where in Outlook are you entering a formula that is not in VBA?
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3
    I'm confused. I've never used Outlook. In fact I don't even know what it's used for.

  4. #4
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    I'm confused too You posted this in "Outlook Help". Perhaps you meant to post it in the Excel section? I moved it over for you.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    No, as long as you bear in mind that it produces a zero value, so just make sure that it's not in a place in the formula where a zero would materially affect the result of the fomula.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Not a bad idea sid. Here's a wee sub to add a comment to an existing formula
    [vba]
    Sub AddComment()
    Dim txt As String
    txt = "+N(" & Chr(34) & InputBox("Enter formula comment") & Chr(34) & ")"
    ActiveCell.Formula = ActiveCell.Formula & txt
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This technique works fine in a numeric formula such as

    =SUM(A1:A7)+N("Sum of amounts")

    but fails miserably with text output such as

    =A2&" should be paid on: "&TEXT(B2,"mm/dd/yyyy")&N("due date of payment")

    but this can also be handled by outputting that as text also

    =A2&" should be paid on: "&TEXT(B2,"mm/dd/yyyy")&TEXT(N("due date of payment"),"")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Hi CD,
    My only concern about this approach is that every remark represents a function call. This will add to the calculation time when the workbook calculates, as well as adding to the complexity of the formula itself. If I saw someone doing this at work, I would want to know why they didn't just use the already built in comment feature? (You can also make your formula's easier to understand by using named ranges.) It's a clever trick, but perhaps a little too clever.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    How about
    =CHOOSE(1, desiredFormula, "any comment")

Posting Permissions

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