Jeremy posed a problem that is based on two cells, A1 and C1. These cells are designed to contain inches and millimeters, respectively. Jeremy wants a way that someone could enter a value in A1 and it would be converted to millimeters in C1. Conversely, they could enter a value in C1 and it would be converted to inches in A1.
Doing the conversion, of course, isn't the real issue. The problem is that if someone enters a value in A1, that value will overwrite any formula that may be in that cell, and mean that any subsequent value entered in cell C1 would not give the required conversion in the previously overwritten A1.
There are a couple of different ways that this could be approached. If you don't mind expanding your worksheet design to include two more cells, those cells could be used strictly for input and cells A1 and C1 could be used strictly for output. One of the input cells could contain the value to be converted and the other could contain the measurement unit of the input value (in or mm, for instance).
Of course, if you want to really limit yourself to two cells, then you will need to resort to using macros to do the actual conversion. You can use a worksheet event that is triggered every time a cell value is changed, and the event handler could check to see if the cell being changed is either A1 or C1. The following macro gives an example of how this could work:
Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False With ActiveSheet If Target = .[A1] Then .[C1].Value = .[A1].Value * 25.4 ElseIf Target = .[C1] Then .[A1].Value = .[C1].Value / 25.4 End If End With Application.EnableEvents = True End Sub
Note that you don't have to have any formulas in cells A1 or C1; the formulas are in the macro itself. If there is a change in cell A1 (inches are entered by the user), then the value in cell C1 is changed by the macro. Likewise, if there is a change in cell C1 (millimeters are entered by the user), then the value in cell A1 is changed by the macro. A change in any other cell besides A1 or C1 is ignored by the macro.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9581) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Developing Reciprocal Conversion Formulas.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Need to get at the next-to-last value in a column, regardless of how many cells are used within that column? This tip ...
Discover MoreWant to figure out how far it is between two points on the globe? If you know the points by latitude and longitude, you ...
Discover MoreWant to add up all the digits in a given value? It's a bit trickier than it may at first seem.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2014-06-23 08:56:28
JC
Willy, your code and humility, as usual, are excellent.
2014-06-22 18:05:48
Alec Whatmough
Another option is to leave A1 and C1 as the output cells, and use B1 as the input cell. So if A1 is Inches, and C1 is centimetres, you enter a number in B1, then look left or right for the converted value.
And, of course, protect A1 and C1...
2014-06-21 08:19:24
Willy Vanhaelen
The macro in this tip isn't well thought-out.
The change event can of course only occur when the sheet is active, so 'With ActiveSheet' isn't needed here.
The macro should check which cell triggered the change event. Instead 'If Target = .[A1] Then' checks if the contents of Target is the same as the contents of cell A1. That can lead to erroneous results. For instance try to enter 10 in cell A1. Cell C1 will show 254 (correct). Now enter 10 in cell C1: nothing happens, cell C1 still shows 254 because 'If Target = .[A1] Then' is true but that is not what we should be testing for!.
As usual, my macro is shorter and does its job correctly.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Select Case Target.Address
Case "$A$1": [C1] = [A1] * 25.4
Case "$C$1": [A1] = [C1] / 25.4
End Select
Application.EnableEvents = True
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 © 2021 Sharon Parq Associates, Inc.
Comments