Seeing the Difference on the Status Bar

by Allen Wyatt
(last updated July 18, 2020)

3

Mike frequently needs to see the difference between two cell values. He can enter a simple "=A1 - B1" formula into a spare cell, but it would be so much easier if Mike could just select the two cells of interest and see the difference, as with sums, averages, etc., in the Status Bar.

Excel allows you to customize what appears on the Status Bar (right-click the Status Bar and choose the statistic you want displayed there), but it does not allow you to display the difference between two cells. This makes a bit of sense, if you think about it—the statistics available for display on the Status Bar are based on however many cells you have selected, and the "difference" works with only two cells.

So, the next logical choice is to either do a formula (as Mike suggests) or use a macro. If you want to go the macro route, the following is a nice, simple approach:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim v As Variant
    Dim c As Range
    Dim sTemp As String

    If Target.Count = 2 Then
        v = 0
        sTemp = ""
        For Each c In Target
            If IsNumeric(c) Then
                v = c - v
            Else
                sTemp = " (non-numeric values in selected range"
                sTemp = sTemp & "; result may be meaningless)"
            End If
        Next c
        Application.DisplayStatusBar = True
        Application.StatusBar = "Diff: " & Abs(v) & sTemp
    Else
        Application.StatusBar = False
    End If
End Sub

The macro is an event handler, designed to reside in the code window for a specific worksheet. (Right-click the worksheet's tab and choose View Code from the resulting Context menu.) To use the macro, just select two cells. The result is shown at the left side of the Status Bar. Note that the macro will also indicate in the Status Bar if either of the cells you selected contains something other than a numeric value.

If you want to create a more complex version of the macro (and use it as an Excel add-in), then you might enjoy this article on the Chandoo website:

https://chandoo.org/wp/customize-excel-status-bar-vba/

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7433) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Looking Up Names when Key Values are Identical

Need to look up some values based upon some key items that may be identical to each other? Depending on the ...

Discover More

Listing Combinations

You can easily use the COMBIN worksheet function to determine the number of combinations that can be made from a given ...

Discover More

Changing Behavior for Audio CDs

When you place an audio CD in you PC, Windows attempts to play the CD, by default. If you don't want Windows to behave in ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Renaming Worksheets Based On a List

Renaming a worksheet within a macro is a relatively easy task. When you start renaming based on a range of names, though, ...

Discover More

Converting Numbers to Strings

When creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...

Discover More

Writing a Macro from Scratch

Creating macros can help extend what you can do in Excel. If you work with macros, you know that creating macros from ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 0?

2020-07-22 12:09:34

Phil W

@J.Wooley

Re "Sorry, but your method only works if the two cells are vertically adjacent."

It does work on two selected cells either in a row or a column. I agree it does not work on non-contiguous selected cells. Below is a modification that handles any two selected cells, contiguous or not.

In playing around with referencing cells within a selection, I noticed that how you select the cells makes a difference, even if the two cells are contiguous.

For example, if I click Cell A1 and drag to cell B1, the selection address is $A$1:$B$1. However if I click on cell B1 first and then select cell A1 by pressing the Ctrl key and clicking on A1, I get a different selection address: $B$1,$A$1.

This is important as the sign of the difference could be affected by how you select the two cells.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Displays the difference between two selected cells on the status bar.
' Modified 7/22/2020 to handle two non-contiguous cells

Dim c As Range
Dim sTemp As String
Dim Count As Integer
Dim Values(2) As Variant

If Target.Cells.Count = 2 Then
sTemp = ""

For Each c In Target
Count = Count + 1
If IsNumeric(c) Then
Values(Count) = c
Else
Values(Count) = 0
sTemp = " (non-numeric values in selected range"
sTemp = sTemp & "; result may be meaningless)"
End If
Next c
Application.DisplayStatusBar = True
Application.StatusBar = "Diff: " & Values(1) - Values(2) & sTemp
Else
Application.StatusBar = False
End If
End Sub


2020-07-21 11:17:19

J. Woolley

@Phil W
Sorry, but your method only works if the two cells are vertically adjacent. Here is another way to display a signed difference. This method applies to all sheets in the workbook, so the three event procedures must be located in the code window of the ThisWorkbook document module.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Selection.CountLarge = 2 Then
Call Workbook_SheetSelectionChange(Sh, Selection)
End If
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Application.StatusBar = False
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim v As Variant, c As Range
If Target.CountLarge = 2 Then
v = 0
For Each c In Target
If IsNumeric(c) Then
v = c - v
Else
v = -v
End If
Next c
Application.DisplayStatusBar = True
Application.StatusBar = "Difference: " & -v
Else
Application.StatusBar = False
End If
End Sub

(see Figure 1 below) is a screen shot of the code window.

Figure 1. Figure 1


2020-07-20 08:01:39

Phil W

If you want to display a signed difference between two values, and assuming you are always subtracting the second value in the selected range from the first one, here is a variation on the macro. This would more closely match the formula mentioned in Allen's comments.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Diff As Variant
Dim sTemp As String

If Target.Count = 2 Then
Diff = 0
sTemp = ""
If Not IsNumeric(Target(1)) Or Not IsNumeric(Target(2)) Then
sTemp = " (non-numeric values in selected range"
sTemp = sTemp & "; result may be meaningless)"
End If

If IsNumeric(Target(1)) Then ' Get first value in selected range
Diff = Target(1)
End If
If IsNumeric(Target(2)) Then ' Subtract the second value from the first value in selected range
Diff = Diff - Target(2)
End If

Application.DisplayStatusBar = True
Application.StatusBar = "Diff: " & Diff & sTemp
Else
Application.StatusBar = False
End If
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.