Fix Python in Excel Returning None or 0 Instead of a Number
Question details
The user is attempting to calculate an average excluding outliers using Python in Excel, but the formula cell outputs 'None' or '0' instead of the calculated numerical average.
- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Running a Python script within an Excel cell to process data and output a calculated statistical average.
- Observed behavior
- The Excel cell displays 'None' or '0' because the script either uses a print statement instead of explicitly returning the value, or because incorrect code indentation prevents the calculation logic from executing properly.
Ensure you are actively connected to the internet, as Python in Excel runs securely in the Microsoft Cloud, and verify that your target dataset contains actual numeric values without hidden text strings.
Return the Calculated Value Instead of Printing
Python cells in Excel require the script to evaluate and output the final variable directly, rather than using standard print statements.
When writing Python code in standard IDEs, developers often use the print() function to see outputs in the console. However, Python in Excel binds the output of the cell to the final evaluated expression in the script. Using a print() command simply sends that value to the background diagnostic console, leaving the cell itself with a 'None' return value.
Select the cell displaying 'None' or '0' and click into the formula bar or press F2 to enter edit mode.
Scroll to the end of your Python script and look for a line similar to print(average) or print(result).
Delete the print() wrapper and leave only the variable or final expression. For example, change 'print(average)' to 'round(average, 4)'.
Press Ctrl+Enter to execute the updated Python code and verify that the cell now correctly displays the numerical average.
Correct Code Indentation for Conditional Logic
Python relies on strict indentation rules to define code blocks. Incorrect spacing can cause the script to skip crucial calculations entirely.
Looking for a Lightweight, Cost-Free Alternative to Microsoft Excel?
While Python in Excel offers advanced programmatic capabilities, it requires a paid Microsoft 365 subscription and a persistent cloud connection. If you need a fast, reliable, and completely free solution for advanced data analysis and spreadsheet management, WPS Spreadsheet is an excellent alternative.
- 1. Download the software: Visit the official WPS Office website and download the free installer for your operating system.
- 2. Install and launch: Follow the on-screen instructions to install WPS Office, then open the WPS Spreadsheet application.
- 3. Open existing files: Easily open your existing .xlsx workbooks and continue your data analysis with seamless compatibility.

Frequently Asked Questions
Why does my Python script work in a standard IDE but return None in Excel?
Standard Python environments use the print() function to display text in a terminal or console. However, Python in Excel requires the script to evaluate an expression at the very end to pass the value back to the Excel cell. If the script ends with a print() command, there is no evaluable return value, resulting in 'None'.
Where does the output from a print() statement go in Excel?
If you use a print() statement in a Python in Excel cell, the output is sent to the Diagnostics task pane. You can view it by clicking the error icon or opening the Diagnostics panel from the formula bar to check for background debugging messages.
How can I avoid indentation errors in the Excel formula bar?
The Excel formula bar isn't a full-fledged IDE, so it can be easy to misalign code. It is recommended to consistently use the spacebar (4 spaces per level) instead of the Tab key. Expanding the formula bar (Ctrl+Shift+U) can also give you a better view of your code structure.




