To calculate a value from y = mx + b in Excel, put the slope m, the intercept b, and the x-value in cells, then use a formula such as =D2*F2+D3. If you have measured x-y data and need Excel to find m and b, use the SLOPE and INTERCEPT functions; Excel does not have a built-in worksheet function named “linear equation solver.”
What y = mx + b means
The equation y = mx + b describes a straight line. The value x is the input, y is the corresponding output, m is the slope, and b is the y-intercept—the value of y when x is zero. For example, in y = 3x + 2, every increase of 1 in x increases y by 3, and the line crosses the y-axis at 2.
There are two common spreadsheet tasks that people call “solving” this equation:
- Evaluate the equation: m and b are known, and Excel calculates y for one or more x-values.
- Fit a line to data: you have observed x-y pairs and want Excel to estimate the slope and intercept that best describe them.
These tasks use different formulas. The first is ordinary arithmetic. The second is linear regression, which finds a best-fit line when the observations do not all fall exactly on one line.
Calculate y when m, b, and x are known
Set up a small input area so the equation is easy to change. For example, enter the slope 3 in D2, the intercept 2 in D3, and an x-value of 4 in F2. In another cell, enter the formula below. With those cell values, the result is 14 because 3 × 4 + 2 = 14.
With the slope in D2, x in F2, and the intercept in D3, enter:
=D2*F2+D3
Cell references make the calculation reusable: change F2 to another x-value and Excel recalculates y. You can also put x-values down a column and copy the formula down beside them. If the slope and intercept should remain fixed while copying, use absolute references: with m in D2, b in D3, and the first x-value in A2, enter =$D$2*A2+$D$3 in B2 and fill it down. The dollar signs keep the parameter references fixed while A2 changes to A3, A4, and so on.
Find the slope and intercept from data
For measured data, place each x-value in one column and its matching y-value in the next. Here is a simple example: A2:A5 contains x-values 1, 2, 3, and 4; B2:B5 contains y-values 5, 8, 11, and 14. These points lie exactly on y = 3x + 2.
With known y-values in B2:B5 and corresponding x-values in A2:A5, enter this formula in D2 to calculate the slope. It returns 3 for the example:
=SLOPE(B2:B5,A2:A5)
With the same y-values in B2:B5 and x-values in A2:A5, enter this formula in D3 to calculate the intercept. It returns 2:
=INTERCEPT(B2:B5,A2:A5)
The order of the arguments matters: both functions take known y-values first and known x-values second. Keep each x-value on the same row as its corresponding y-value. Once D2 and D3 contain the calculated slope and intercept, use the prediction formula from the previous section to estimate y for new x-values.
The functions use all the supplied observations, not just the first and last points. For imperfect real-world data, Excel calculates the line that minimizes the sum of the squared vertical differences between observed y-values and the line’s predicted y-values. That is why the resulting line may not pass through every point.
Calculate the line from two points
If you have exactly two points and their x-values differ, you can calculate the line directly. For example, put the first point’s x and y in A2 and B2, and the second point’s x and y in A3 and B3. With those four cells, calculate m in D2 using:
=(B3-B2)/(A3-A2)
For points (1, 5) and (4, 14), this returns a slope of 3. With the first point in A2 and B2 and the slope in D2, calculate b in D3 using:
=B2-D2*A2
This returns 2, so the equation is y = 3x + 2. The two-point method is useful for an exact line through two known points, but it is sensitive to measurement error. If you have more than two observations, SLOPE and INTERCEPT provide a regression line that uses all of them.
Display the equation on an Excel chart
A scatter chart is a useful visual check because it plots numerical x-values against numerical y-values. Select the two data columns, including their headers if present, then choose Insert > Insert Scatter (X, Y) or Bubble Chart and select a scatter option. In Microsoft 365 for Windows, select the chart and use Chart Design > Add Chart Element > Trendline > More Trendline Options. In the Format Trendline pane, choose a linear trendline and select the option to display the equation on the chart.
Excel may display the equation with rounded coefficients. Treat that label as a convenient visual summary, not necessarily as the most precise source for calculations. Use SLOPE and INTERCEPT in worksheet cells when you need their full stored precision, and format the cells to show more decimal places if needed.
Use an XY scatter chart rather than a standard line chart when x-values represent numerical measurements with meaningful intervals. A line chart can treat its horizontal entries as categories, which may space values such as 1, 2, and 10 evenly even though their numeric distances differ. On Excel for Mac, the chart and trendline options are available through the chart design and formatting controls, though their placement can differ from the Windows ribbon. Excel for the web has chart and trendline capabilities, but some formatting controls may differ from desktop Excel.
Solve for x when y is known
Sometimes the unknown is x rather than y. Rearrange the equation to x = (y - b) / m. For example, if the line is y = 3x + 2 and the target y-value is 14, then x is 4.
With m in D2, b in D3, and the target y-value in F3, enter this formula to calculate x. With F3 equal to 14, it returns 4:
=(F3-D3)/D2
This calculation requires a nonzero slope. If D2 is zero, the line is horizontal: it never reaches a different y-value, and if the target equals its constant y-value, every x-value works. A formula that divides by a zero slope will return Excel’s division-by-zero error, so check that m is not zero before interpreting the result.
Common errors and how to avoid them
- Reversing x and y:
SLOPEandINTERCEPTexpect y-values first, then x-values. Reversing them fits a different relationship. - Misaligned rows: each x and y pair must refer to the same observation. Sorting one column alone can produce a plausible but incorrect result.
- Using a category chart: a line chart is not always the right representation for numeric x-values. Use an XY scatter chart to preserve the x-axis scale.
- Assuming a perfect fit: with noisy measurements, the fitted line is an estimate. Residuals—the differences between observed and predicted y—will usually remain.
- Forgetting the intercept:
y = mxassumes b is zero. Do not omit b unless the relationship genuinely must pass through the origin. - Including headers or unrelated cells: select only the numeric observations for function arguments. Ensure the two ranges have the same number of rows.
- Relying on rounded chart text: the displayed trendline equation may be rounded. Reference worksheet calculations for more precision.
Blank cells, text, and errors in the data can also complicate analysis. Check for missing observations and make sure each pair is numeric before interpreting the result. If the relationship curves, a straight-line equation may be a poor model; a high or low fit statistic alone does not establish that a linear model is appropriate.
Do you need Excel Solver?
Usually, no. The Solver add-in is an optimization tool: it adjusts selected decision-variable cells to maximize, minimize, or reach a target subject to constraints. It is useful for larger optimization problems, but it is unnecessary for calculating y from known values or fitting a straight line with SLOPE and INTERCEPT.
If your Data tab does not show Solver and you specifically need the add-in, in Excel for Microsoft 365 on Windows go to File > Options > Add-ins. Beside Manage, choose Excel Add-ins, select Go, check Solver Add-in, and choose OK. Solver is then available from the Data tab. On Mac, add-ins are managed from Tools > Excel Add-ins; availability and exact controls can vary by Excel release. Solver is not a substitute for the simple formulas above.
Frequently Asked Questions
What is the Excel formula for y = mx + b?
Use multiplication and addition with references to the cells holding m, x, and b. For example, if m is in D2, x is in F2, and b is in D3, use =D2*F2+D3.
How do I get m and b from a table of values?
Put x-values and matching y-values in separate columns. Use =SLOPE(y_range,x_range) for m and =INTERCEPT(y_range,x_range) for b, replacing the ranges with your actual cells.
Can Excel find a linear equation from two points?
Yes. Calculate the slope as the change in y divided by the change in x, then calculate b as y minus slope times x. The points must have different x-values for a unique, nonvertical line in this form.
How do I solve y = mx + b for x in Excel?
Rearrange it to x = (y-b)/m. In a worksheet, subtract the intercept from the target y-value and divide by the slope. The slope must not be zero.
Why does the chart equation differ slightly from my cell result?
The chart label can round its displayed coefficients, while worksheet functions retain more precision. Use the function results in cells for calculations and increase displayed decimal places when comparing values.
Related
- Excel Not Responding? Causes and Fixes
- How to Enter a Page Break in Excel and Control Printing
- Excel Data Validation Lists Based on Criteria
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Yes/No Dropdown in Google Sheets: Add One in MinutesTo add a Yes/No dropdown in Google Sheets, select the cell or cells, choose Insert > Dropdown, enter…
- Currency Exchange in Google Sheets: Rates and FormulasTo get a currency exchange rate in Google Sheets, enter a formula such as =GOOGLEFINANCE("CURRENCY:USDEUR"), which requests the…
- Create Rules in Outlook for Gmail: A Step-by-Step GuideTo create a rule for Gmail in classic Outlook for Windows, open File > Manage Rules & Alerts…
- Find Circular References in ExcelTo find a circular reference in Excel for Microsoft 365 on Windows, open Formulas > Error Checking >…
- Change Date Format in Excel: General Format ExplainedTo change dates showing as numbers in Excel, select the cells and choose Home > Number Format >…
- Archive Outlook Emails to an External Hard DriveYes. In classic Outlook for Microsoft 365 on Windows, export email to an Outlook Data File (.pst), then…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.