cellmatrix.net

Formula Trace Tool Update

I've recently updated the Formula Trace Tool utility to include a few new features. Behind the scenes, the formula auditing code has been streamlined to run more efficiently. If interested, please visit the Downloads page for more information.

Building the Formula Trace Tool Post 3

In the final post of this series, I've completed the first version of the formula trace tool by adding the following:

  • The option to trace dependent cells.
  • A "Step" function that allows the user to move through precedent or dependent cell references. It reactivates the tool from any precedent or dependent cell reference.
  • A "Clear" function that allows the user to clear the audit arrows without closing the tool.
  • A command button to activate the tool. The command button symbol is a blue arrow pointing to the right over a white background.

The first version of the tool looks like this:

image

The tool can be used as a regular spreadsheet file (.xls) or as an add-in (.xla). To use as a spreadsheet file, open TraceTool.xls as you would any other spreadsheet. The command button with the blue arrow should appear. Then open the spreadsheet that you want to audit. To use the tool as an addin, open TraceTool.xls and then save it as TraceTool.xla.

You can download the utility via the link below:

To activate the tool, click on the command button:

image

Building the Formula Trace Tool Post 2

As a recap to Building the Formula Trace Tool Post 1, a macro was created that records the cell address and worksheet name for each precedent cell based on the active cell address. Each precedent cell address and worksheet name are then converted into a test string and then loaded into an array.

The next steps are to load the results of the array into a listbox. To do so, first I created a userform that contains a listbox (shown below in the design view). The userform is called "frmTrace" and the listbox within that userform is called "Listbox1". I've also added a command button (Exit) to right side bottom of the userform called "btnExit".

image

After creating the userform, I added the following code to the code module to initialize it.

Private Sub UserForm_Initialize()

    Application.ScreenUpdating = False

    frmTrace.StartUpPosition = Manual
    frmTrace.Top = 50
    frmTrace.Left = 500

    Call TracePrecedents

    Application.ScreenUpdating = True

End Sub

To show the userform I added a command button to the worksheet. That command button is named "CB1" and captioned "Trace Formulas". I added the following code to the command button to show the userform.

Private Sub CB1_Click()
    frmTrace.Show
End Sub

When the userform is initialized, it calls a procedure called "TracePrecedents". The TracePrecedents procedure is what was originally presented in Building the Formula Auditing Tool Post 1. After some more work, that procedure has been modified to the one below and added to the "frmTrace" code module.

Private Sub TracePrecedents()

    On Error Resume Next

    Dim iAddress As String
    Dim UniqueValues As New Collection
    Dim iArrow As Integer

    Set Origin = ActiveCell

    Origin.ShowPrecedents

    For iArrow = 1 To 200

        For iLink = 1 To 200

            Err = 0

            Set oPrec = Origin.NavigateArrow(True, iArrow, iLink)

            Cell = oPrec.Address
            Sheet = oPrec.Parent.Name
            Workbook = oPrec.Parent.Parent.Name
            iAddress = "[" & Workbook & "]" & Sheet & "!" & Cell
            UniqueValues.Add iAddress, CStr(iAddress)

            If Err <> 0 Then Exit For

        Next

    Next

    frmTrace.ListBox1.RowSource = ""
    For Each Item In UniqueValues
        frmTrace.ListBox1.AddItem Item
    Next Item

End Sub

At this point the cell references will load into the listbox. However, code still needs to be added that programs the listbox to respond when a cell reference is activated. The procedure below parses the text string into a workbook, worksheet, and cell reference component. Each time the list box is clicked, the event fires.

Private Sub ListBox1_Click()

    Dim iSeg1, iSeg2 As Integer
    Dim iCell, iSheet, iWorkbook As String

    Item = ListBox1.Value

    iSeg1 = Application.WorksheetFunction.Find("]", Item)
    For i = 1 To Len(Item)
        iWorkbook = Mid(Item, 2, iSeg1 - 2)
    Next i

    iSeg2 = Application.WorksheetFunction.Find("!", Item)
    For i = 1 To Len(Item)
        iCell = Mid(Item, iSeg2 + 1, i)
    Next i

    For i = 1 To Len(Item)
        iSheet = Mid(Item, Len(iWorkbook) + 3, Len(Item) _
        - Len(iWorkbook) - Len(iCell) - 3)
    Next i

    Workbooks(iWorkbook).Activate
    Worksheets(iSheet).Select
    Range(iCell).Select

End Sub

In the screenshot below, I've activated the userform by hitting the Trace Formulas button. The active cell is C21 as indicated by the blue background for the cell reference "[TraceTool.xls]Sheet1!$C$21" in the userform. The precedent references for cell C21 are indicated by the blue tracer arrows and are loaded as a list in the userform. As I click on each cell reference in the userform, that cell reference will be selected via the click event shown above.

image

Finally, I've added code to exit the userform and clear the auditing arrows. The procedure below fires when the Exit button is clicked.

Private Sub btnExit_Click()
    ActiveSheet.ClearArrows
    Unload Me
End Sub

The procedure below fires if the userform is terminated in a way other than using the Exit button.

Private Sub UserForm_Terminate()
    ActiveSheet.ClearArrows
End Sub

In the next post I plan to add code to trace dependent cells and possibly add a feature that allows the user to step through formulas.

References:
  • John Walkenbach's book titled Excel 2003 Power Programming with VBA - Part IV - served as a reference for how to create the userform, listbox, and command button for the Trace Tool add-in.

Building the Formula Trace Tool Post 1

I spend a good deal of time building and auditing spreadsheets in my position as an analyst in a Finance department. One of the features that has always frustrated me about Excel is the program's ability to audit the spreadsheet. Specifically, the need to double-click on the auditing arrows to move to precedent or dependent cells is, in my opinion, very difficult and inefficient.

Aaron Blood's Explode Add-In converts all of the precedent or dependent cells into cell addresses that appear in a listbox. You can then click on the listbox addresses to move to each cell address in the spreadsheet. The Explode Add-In is a great idea. Unfortunately, the code is protected so I couldn't customize it to my needs.

In this and future posts, I'm going to attempt to create my own formula auditing tool. Like the Explode Add-In, I want to be able to click on formula references to move between precedent and dependent cells. My other qualifications are that the add-in needs to be simple, efficient, and easy to use.

The first challenge to building the Formula Trace Tool add-in is to come up with the code that converts the precedent and dependent cell references to cell addresses. That code is below.

Sub TracePrecedents()

    On Error Resume Next

    Dim Rng As Range
    Dim Adrs As String
    Dim UniqueValues As New Collection
    Dim iArrow As Integer

    Set Origin = ActiveCell

    Origin.ShowPrecedents

    For iArrow = 1 To 1000
        
        Err = 0
        
        Set oPrec = Origin.NavigateArrow(True, iArrow)
        Adrs = oPrec.Parent.Name & " - " & oPrec.Address
        UniqueValues.Add Adrs, CStr(Adrs)
        
        If Err <> 0 Then Exit For

    Next

    Set Rng = ActiveSheet.Range("E3")

    For Each Item In UniqueValues
        Rng.Value = Item
        Set Rng = Rng.Offset(1, 0)
    Next Item

End Sub

To view the output of the procedure, set the active cell to a cell with precedents and run the macro. In the screenshot below, the active cell is C22 and the precedent cells are indicated by the blue auditing references. Each precedent cell is listed in the range E3:E9.

image

Alternatively, get rid of the range references and work in the immediate window. The resulting code is below.

Sub TracePrecedents()

    On Error Resume Next

    Dim Rng As Range
    Dim Adrs As String
    Dim UniqueValues As New Collection
    Dim iArrow As Integer

    Set Origin = ActiveCell

    Origin.ShowPrecedents

    For iArrow = 1 To 1000
        
        Err = 0
        
        Set oPrec = Origin.NavigateArrow(True, iArrow)
        Adrs = oPrec.Parent.Name & " - " & oPrec.Address
        UniqueValues.Add Adrs, CStr(Adrs)
        
        If Err <> 0 Then Exit For

    Next

    For Each Item In UniqueValues
        Debug.Print Item
    Next Item

End Sub

As a sidenote, Microsoft provides a list of error codes as well as selected error handling procedures.

The next step is to try to replace the "Debug.Print Item" statement with some type of listbox load. I'll attempt to address that in the next post of this series.