Written by Allen Wyatt (last updated July 18, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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 Microsoft 365.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
One of the most common ways of creating macros is to use Excel's macro recorder. This tip shows how easy it is to use the ...
Discover MoreThe data stored in a worksheet can often correspond to information external to that worksheet. For instance, you might ...
Discover MoreCopying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments