Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Function Indenting

Latest post Fri, Mar 28 2008 6:56 AM by Anonymous. 13 replies.
  • Mon, Feb 4 2008 4:43 PM

    • Jim Thomlinson
    • Top 10 Contributor
    • Joined on Thu, Jan 10 2008
    • Vancouver, Canada
    • Posts 84
    • Points 1,256

    Function Indenting

    It would be nice if you could write a function with indenting through some inherant XL feature so that whe you start writing nested if functions it is much more intuitive how the flow of the logic works.

    • Post Points: 21
  • Tue, Feb 5 2008 1:45 PM In reply to

    Betreft: Function Indenting

    I know someone has written a tool that does exactly that, but I can't for the life of me find it. Did you search google for formula formatting or similar queries?
    Regards, Jan Karel Pieterse www.jkp-ads.com
    • Post Points: 37
  • Tue, Feb 5 2008 3:02 PM In reply to

    • Jim Thomlinson
    • Top 10 Contributor
    • Joined on Thu, Jan 10 2008
    • Vancouver, Canada
    • Posts 84
    • Points 1,256

    Function Indenting

    I've seen a couple of tools for doing that. Some better than others but I never found one that I thought was a seemless integration including function indenting along with indications on the spreadsheet of the selected ranges (similar to when you edit a formula you get coloured outlines of the selected ranges). The ability to add comments would be nice. Even nicer would be the ability to highlight sections of the formula and have a pop up show what the returned value of that section of the formula would be. Additionally I would like to see the functions themselves highlighted in a colour such as Blue similar to in the VBE when you add a function...

    In short I would like XL to allow me to write functions in a cell the way a programmer would (should) write functions in the VBE... And I want that to come standard right out of the box.

    • Post Points: 21
  • Wed, Feb 6 2008 12:23 AM In reply to

    Betreft: Function Indenting

    Sounds like a feature request for the next Excel release!

    Regards, Jan Karel Pieterse www.jkp-ads.com
    • Post Points: 21
  • Wed, Feb 6 2008 5:54 AM In reply to

    Re: Function Indenting

     Jim,

     

    I think you may be asking for a monster.

     

    One of the things about functions is that they can easily get over-cooked, because we can, we  do. IMO giving mor than 7 nested functions was a mistake because instead of re-designing the data,m the formulae, et al, bad developers added more functions to already complex formulae. This sort of functionality can only be necessary when the formulae are complex. Whilst I admit in these circumstances, I will format the formula with line breaks and white-space to make it easier to read, I don't think encouraging me to over-develop formulae is the way to go.

     

    Just my 2d's worths (old money!) 

    Regards

    Bob

    • Post Points: 52
  • Wed, Feb 6 2008 6:00 AM In reply to

    Re: Function Indenting

    BTW, you can add comments now

     

    =SUM(D3:D5)+N("This function adds 3 cells")

     

    and

     

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

     

    But it would be nice to have some sort of meta-content. 

    Regards

    Bob

    • Post Points: 5
  • Wed, Feb 6 2008 11:31 AM In reply to

    • Jim Thomlinson
    • Top 10 Contributor
    • Joined on Thu, Jan 10 2008
    • Vancouver, Canada
    • Posts 84
    • Points 1,256

    Re: Function Indenting

    Bob Phillips:
    One of the things about functions is that they can easily get over-cooked

    Agreed that we do at times overdo things a tad (heavy sarcasm on the word tad Indifferent). But necessity being what it is sometimes there is just no easy way around it... We give the SQL folks tools that allow them to write queries in a structured way. Why not the XL folks? Could you not use the same argument that by giving SQL a structured developement environment we have encouraged them to develope overly complicated queries?

    Do I think that providing a structured environment will cause XL users to write even more complicated formulas. I doubt it. If you are inclined to write a complicated formula then you are probably going to do it either way. A structured environment is just going to make it easer to debug and get it right...

    And that is my 2 cents

    • Post Points: 5
  • Wed, Feb 6 2008 1:46 PM In reply to

    • hrlngrv
    • Top 10 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 67
    • Points 1,055

    Re: Function Indenting

    Bob Phillips:

    ...
    . . . IMO giving mor than 7 nested functions was a mistake . . .
    ...

    Why is 7 a magic number? Just because that's what it's been for s few decades?

    And if 7 levels is OK, why not provide the tools that would make it easier AND LESS ERROR-PRONE to use those 7 levels? Especially when one is trying to figure out what someone else's formulas are.

    It's a pain that Excel provides a formula auditor but no formula profiling tools, e.g., function call profiling, interworksheet referencing profiling, an easy way to check whether all cells in the selected range have EXACTLY the same formula using R1C1 referencing.

    I've mentioned this in the Excel team's blog over a year ago, but it'd be nice to have PARAMETRIZED NAMES, that is, names that accept arguments and perform simple text substitution of those parameters in the definition. Also add a comments property to the Name class and make it possible to add comments for names in the Define Name dialog. Those two things would go a long way towards eliminating complex formulas in cells. Note: resolving parametrized names COULD be handled at formula entry/parsing time, with the stored formula using the definition of the parametrized names, so recalc speed would be unaffected.

    • Post Points: 5
  • Wed, Feb 6 2008 2:05 PM In reply to

    • Jim Thomlinson
    • Top 10 Contributor
    • Joined on Thu, Jan 10 2008
    • Vancouver, Canada
    • Posts 84
    • Points 1,256

    Re: Function Indenting

    Bob Phillips:
    MO giving mor than 7 nested functions was a mistake

    On one level I agree and on another level I disagree. Any function with more than 7 if's is getting to be a little over the top. That is equally true of XL formulas as it is of VBA code. To that end in VBA we have the Select Case structure which greatly simplifies logical flow. In XL we tend to use lookups to get around this. Why not Give XL a select case type function. The only good reason that I can see is because without indenting and such the formula will become unmanageable in no time flat. If a structure was there then it could become managable.

    I think I must be up to 4 cents by now...

    • Post Points: 21
  • Wed, Feb 6 2008 7:20 PM In reply to

    Re: Function Indenting

     I think that both of you are arguing for something because Excel doesn't have the thing that you really want <g>.

     Because it lacks a decent facility is no reason to make bad facilities simpler and easier to use.

     

    Regards

    Bob

    • Post Points: 37
  • Wed, Feb 6 2008 7:42 PM In reply to

    • Jim Thomlinson
    • Top 10 Contributor
    • Joined on Thu, Jan 10 2008
    • Vancouver, Canada
    • Posts 84
    • Points 1,256

    Re: Function Indenting

    Darn right I am arguing for something because XL is missing the thing that I want. And while we are on the subject, I want a pony too. If you are going to dream... dream big. I would kick myself if my dream came true and I forgot to ask for the pony.

    As for making bad facilities easier to use, I don't think that is what I am asking for. I am asking for good facilities. A facility that allows me to make sense out of a complex formula. I can not count the number of times that I have been auditing a formula trying to match up brackets and commas and... to determine where I have an extra / missing bit of syntax.

    If I understand your argument correctly the worry is that if we make formulas easier to understand people will just start making longer and more complex formulas? But if by structuring the formulas the complexity becomes managable then where is the down side? Always of course keeping in mind that no matter how good of a system is created someone will come along and muck it up well beyond recognition, but then again that is true of most good ideas.

    • Post Points: 21
  • Thu, Feb 7 2008 1:07 AM In reply to

    • hrlngrv
    • Top 10 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 67
    • Points 1,055

    Re: Function Indenting

    Bob Phillips:

    I think that both of you are arguing for something because Excel doesn't have the thing that you really want <g>.

    Because it lacks a decent facility is no reason to make bad facilities simpler and easier to use.

    Point 1 deserves a response like this: if Excel is meant for serious application development, it should have better application development and maintenance tools; if it isn't meant for serious application development, several hundred of its built-in functions should be removed. You call it, Bob.

    As for the second point, first there's the issue of figuring out other people's spreadsheets. Do you mean that you find Excel's current tools more than adequate to figure out any & every spreadsheet you're going to see? Perhaps you might if you're not going to see many, and those you do see are almost certain to be extremely simple. Realize that your idyllic circumstances aren't shared by all who need to work with spreadsheets.

    Or do you mean that long formulas are invariably a bad thing? You're entitled to your opinion. Just don't try to impose it on others. If you don't want to write long formulas, don't. If you don't want Excel 2007's expandable formula bar ever to expand, then keep your formulas short.

    Actually, Microsoft has already ignored/rejected your ideas of proper formulas. Formulas up to 8192 characters in Excel 2007, the expandable formula bar, many more than 7 nested function call levels. Seems like you've already lost the war. Are you carrying on a guerilla operation to try to prevent Microsoft from providing the rest of us the tools necessary to deal with the monster formulas that are already permitted if not encouraged? Gee, thanks, Bob.

    • Post Points: 5
  • Thu, Feb 7 2008 1:54 AM In reply to

    • hrlngrv
    • Top 10 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 67
    • Points 1,055

    Re: Function Indenting

    Jim Thomlinson:

    ...
    I can not count the number of times that I have been auditing a formula trying to match up brackets and commas and...
    ...

    I'd been debating with myself whether to post this or not, but WTH. Somewhat stripped down from what I use,

     
    '-- begin VBA ------
    Option Explicit


    Function foo( _
     rng As Range, _
     Optional bars As Boolean = False, _
     Optional fcnpat As String = "" _
    ) As String
    '-----------------------------------
      'Copyright (c) 2008 Harlan Grove.
      '
      'This code is free software; you can redistribute it and/or modify
      'it under the terms of the GNU General Public License as published
      'by the Free Software Foundation; either version 2 of the License,
      'or (at your option) any later version.
      '-----------------------------------
      'requires a reference to VBScript Regular Expressions 5.5

      Const NS As Long = 4        'should be >= 2
      Const SEP As String = ","   'function argument separator
      'SEP could be queried from the OS - left as an exercise

      Dim k As Long, lvl As Long, f As String, t As String
      Dim nas As Long, nlp As Long, nrp As Long
      Dim sc As Variant, xc As Variant, fc As Variant
      Dim re As RegExp, mc As MatchCollection, m As Match

      If Not rng.Cells(1).HasFormula Then Exit Function

      'only processing 1st cell in rng
      'handling all cells in rng left as an exercise
      f = rng.Cells(1).Formula
      Set re = New RegExp
     
      'first find all string literals . . .
      re.Global = True
      re.Pattern = """[^""]*(""""[^""]*)*"""
      Set mc = re.Execute(f)
      re.Global = False

      '. . . and replace 'em with ad hoc tokens
      If mc.Count > 0 Then
        ReDim sc(1 To mc.Count)
     
        For k = 1 To mc.Count
          sc(k) = mc(k - 1).Value
          f = re.Replace(f, Format(k, "\:\s\t\r00\:"))
        Next k
      End If

      Set mc = Nothing

      'next find all non-function-call parametrized terms . . .
      re.Global = True
      re.Pattern = "(?:[^_A-Z.0-9])\([^()]+\)"
      Set mc = re.Execute(f)
      re.Global = False

      '. . . and replace 'em with other ad hoc tokens
      If mc.Count > 0 Then
        ReDim xc(1 To mc.Count)
     
        For k = 1 To mc.Count
          xc(k) = mc(k - 1).Value
          f = re.Replace(f, Format(k, "\:\p\e\x00\:"))
        Next k
      End If

      Set mc = Nothing

      'now remove all remaining whitespace
      re.Global = True
      re.Pattern = vbLf & " *"
      f = re.Replace(f, "")
      re.Global = False

      'using a state machine to add the indentation
      lvl = 0   'start off at function call level zero
      nas = InStr(1, f, SEP)
      nlp = InStr(1, f, "(")
      nrp = InStr(1, f, ")")
      k = nlp   'if formula is syntactically valid, nlp < nac or nrp
     
      Do While k > 0
        If k = nas Then
          'arg separator, no level change
          f = Left(f, k) & vbLf & String(NS * lvl, " ") & Mid(f, k + 1)
        ElseIf k = nlp Then
          'left paren, begin next level
          lvl = lvl + 1
          f = Left(f, k) & vbLf & String(NS * lvl, " ") & Mid(f, k + 1)
        ElseIf k = nrp Then
          'right paren, end current level
          lvl = lvl - 1
          f = Left(f, k - 1) & vbLf & String(NS * lvl, " ") & Mid(f, k)
        Else  'this shouldn't happen!
          Debug.Assert 1 = 0  'FUBAR
        End If

        Debug.Assert lvl >= 0  'also FUBAR, i.e., shouldn't happen

        k = k + 2 + NS * lvl    'where we start the next char search
        nas = InStr(k, f, SEP)
        nlp = InStr(k, f, "(")
        nrp = InStr(k, f, ")")

        'set k to next arg separator or parenthesis, if any
        'if none of them remaining, k set to 0, so loop will end
        k = nas
        If k = 0 Or nlp < k Then k = nlp
        If k = 0 Or nrp < k Then k = nrp
      Loop

      'find all argument lists that DON'T contain function calls . . .
      re.Global = True
      re.Pattern = "\((\n +[^ (),]*,?)+\)"
      Set mc = re.Execute(f)
      re.Pattern = vbLf & " +"

      '. . . and compress them back to a single line
      'note: this is done back to front because each replacement alters
      'char positions of matches to its right BUT NOT to its left
      For k = mc.Count To 1 Step -1
        Set m = mc(k - 1)
        t = re.Replace(m.Value, "")
        f = Left(f, m.FirstIndex) & t & Mid(f, m.FirstIndex + m.Length + 1)
      Next k

      Set mc = Nothing

      'OPTIONAL! remove indentation of SINGLE function calls within
      'function calls the names of which would be specified in fcnpat
      If fcnpat <> "" Then fc = Split(fcnpat, SEP)

      If IsArray(fc) Then
        For k = LBound(fc, 1) To UBound(fc, 1)
          re.Pattern = "(" & fc(k) & ")\(\n +([^ ]+)?\n *\)"
          f = re.Replace(f, "$1($2)")
        Next k

        Erase fc
      End If

      'OPTIONAL! add vertical bars to indented formula
      If bars Then
        re.Pattern = String(NS, " ")
        f = re.Replace(f, String(NS - 2, " ") & "| ")
      End If

      Set re = Nothing

      'restore non-function-call parenthesized expressions
      If IsArray(xc) Then
        For k = LBound(xc, 1) To UBound(xc, 1)
          f = Replace(f, Format(k, "\:\p\e\x00\:"), xc(k), 1, 1)
        Next k

        Erase xc
      End If

      'restore string literals
      If IsArray(sc) Then
        For k = LBound(sc, 1) To UBound(sc, 1)
          f = Replace(f, Format(k, "\:\s\t\r00\:"), sc(k), 1, 1)
        Next k

        Erase sc
      End If

      foo = f

    End Function


    Sub testit()
      'select a cell containing a nast formula and run this macro
      If Not TypeOf Selection Is Range Then Exit Sub
      MsgBox foo(Selection)
      MsgBox foo(Selection, True)
      MsgBox foo(Selection, True, "IS[A-Z]+,N,T")
    End Sub
    '--  end VBA  ------

    • Post Points: 5
  • Fri, Mar 28 2008 6:56 AM In reply to

    Re: Betreft: Function Indenting

    • Post Points: 5
Page 1 of 1 (14 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.