VBA & Macro Problems

Auto-Convert Millimeters to Inches in the Same Excel Cell with VBA

Emma BrownEmma Brown Oct 29, 2026 870 views

Question details

The user wants to type a millimeter value into a specific Excel cell (such as F4) and have that exact cell automatically overwrite the input with the equivalent value in inches.

How to Auto-Convert Millimeters to Inches in the Same Excel Cell
Product
Microsoft Excel
Device & OS
not provided
Scenario
Entering measurement data in millimeters but needing the final documented value in the spreadsheet to be displayed in inches-of-mercury or standard inches.
Observed behavior
A VBA worksheet event must detect changes to the designated cell, calculate the conversion by dividing the input by 25.4, and replace the original millimeter input with the resulting inch value without causing an infinite loop.
Before you start

Ensure that your workbook is saved in a macro-enabled format (.xlsm) and that you have enabled macros in your spreadsheet application's Trust Center settings before writing VBA code.

Solution 1Recommended

Use a Worksheet_Change Event Macro for Automatic Conversion

Utilize a built-in VBA event to trigger an automatic conversion whenever data is entered into a specific cell.

By utilizing the Worksheet_Change event, Excel can actively monitor a target cell. Once a value is entered, the macro disables system events to prevent an infinite loop, divides the input by 25.4 to convert millimeters to inches, and instantly updates the cell in place.

1
Open the VBA Editor

Right-click the specific worksheet tab at the bottom of your Excel window and select 'View Code' to open the Visual Basic for Applications (VBA) editor.

2
Enter the VBA Code

Paste the following code into the module window: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address <> "$F$4" Then Exit Sub Application.EnableEvents = False Target.Value = Target.Value / 25.4 Application.EnableEvents = True End Sub

3
Save as a Macro-Enabled Workbook

Close the VBA editor and go to File > Save As. Choose 'Excel Macro-Enabled Workbook (*.xlsm)' from the file type dropdown to preserve your macro.

Use a Worksheet_Change Event Macro for Automatic Conversion
Changing the Target Cell: If you need to apply this conversion to a different cell instead of F4, modify the '$F$4' address within the macro code to match your desired cell reference.
Automate with WPS Office

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Office provides robust support for VBA macros, allowing you to automate tasks like unit conversions directly within your spreadsheet. It is fully compatible with Excel files, making it a perfect tool for advanced data tasks.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your .xlsm file containing the conversion macro.
  2. 2. Enable Macros: If prompted, click 'Enable Macros' in the security warning bar below the ribbon to allow the VBA code to run.
  3. 3. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'Visual Basic' to view, edit, or create new macro code.
  4. 4. Test the Conversion: Enter a millimeter value into cell F4, press Enter, and watch WPS Spreadsheet instantly overwrite it with the inch equivalent.
Fully supports Microsoft Excel VBA macros and .xlsm formatsLightweight application with incredibly fast processing speedsFamiliar, tabbed user interface for easy navigation and editingFree to use for everyday office and administrative tasks
microsoft office alternative - wps office

Frequently Asked Questions

How do I convert millimeters to inches without using VBA?

If you prefer not to use macros, you can use the CONVERT function in a separate, adjacent cell. For example, typing =CONVERT(F4, "mm", "in") will display the converted value, but it will not overwrite the original input in cell F4.

Why does my Excel freeze or crash when the macro runs?

This typically happens if 'Application.EnableEvents = False' is omitted from the code. Updating the cell triggers the macro again, causing an infinite loop. Always ensure you disable events before changing the cell value and re-enable them immediately afterward.

Can I apply this macro to a whole column instead of a single cell?

Yes. You can modify the condition to check if the changed cell is within a specific range. Instead of checking for exactly '$F$4', you can use 'If Not Intersect(Target, Range("F:F")) Is Nothing Then' to apply the conversion to the entire F column.

What is the exact mathematical conversion factor for millimeters to inches?

One inch is precisely equal to 25.4 millimeters. Therefore, to convert from millimeters to inches, you must divide your millimeter value by 25.4.