Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Developing Reciprocal Conversion Formulas.
by Allen Wyatt
(last updated June 21, 2014)
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 changes 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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9581) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Developing Reciprocal Conversion Formulas.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you have a large amount of data in a worksheet and you want to extract information from the text that meets certain ...Discover More
Replacing one character in a text value with another character is easy. All you need to do is use the SUBSTITUTE function, as ...Discover More
Sometimes it can be confusing to figure out the source of an error that is displayed in your worksheet. Excel provides a ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.