ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Slope/Intercept (y = mx + b) Linear Equation Auto-Graphing Tool (Free Excel Software)

Updated on April 28, 2012

I’m sure there are apps for this, but if you do not have one this program might be useful; particularly for math students working with linear equations. You will need Microsoft Excel. We will go through how to install the program here. It will involve copying and pasting my code into Excel. Open up Excel, save the new file as “slopeintercept.xlsm”. In newer versions of Windows, you will save as type “Excel Macro-Enabled Workbook”. This will be a spreadsheet with embedded code to calculate points and graph a line. You will need access to the Developers tab in Excel. To make it visible in the new spreadsheet do as follows:

1. File --> Options (click)

2. Customize Ribbon (click)

3. Check the “Developer” box and click “OK” at bottom.

4. You will now see “Developer” tab. Click it.

5. Section dealing with macros and VBA (Visual Basic) is now visible.

We will now create a macro, into which we will later paste some code. Over on the left, find the tab “Record Macro”. A box pops up. It will have been given a default name, just leave it. There will also be a shortcut key entry, make it “ctrl a”. Click OK. Do not press any keys , or you will actually start recording keystrokes in the macro. Rather, immediately click the “stop recording” tab, located in same place where “Record Macro” was. We have created a blank routine into which we will now place some code. Click on “Macros” and you will see the blank macro we just created. Click “Edit”. The macro will open up in a window, see picture in the diagram above. Place the blinking cursor as shown. In order to do so, you will have to down arrow key until the cursor sits next to "End Sub". Hit Enter key 5 times to move "End Sub" down. Up arrow key 3 times to put the cursor between "End Sub" and lines above it. Now copy and paste in this code:

Dim b As Single, m As Single, bstr As String, mstr As String, eqnstr As String, tempstr As String

Dim x1 As Single, y1 As Single, x2 As Single, y2 As Single, x3 As Single, y3 As Single

Dim num As Single, den As Single, numstr As String, denstr As String

Dim mstrlen As Single, bstrlen As Single

On Error GoTo BadCalculation

Sheets("Sheet1").Activate

MsgBox _

"You will be asked to enter slope and y intercept. Fractional form is acceptable," & vbCrLf & _

"using single digit only in NUMERATOR. For example, slope = -10/50 is not acceptable. Use -1/5 instead." & vbCrLf & _

vbCrLf & _

"1/500 or 1/5000 is OK, denominator can be greater than one digit." & vbCrLf & _

"If errant data is submitted, program will end with error message", vbOKOnly

If ActiveSheet.ChartObjects.Count = 1 Then

ActiveSheet.ChartObjects(1).Delete

Else

End If

mstr = InputBox("Enter slope: i.e. -1/2, 5, 0.3")

mstrlen = Len(mstr)

If InStr(mstr, "/") <> 0 And Mid(mstr, 1, 1) = "-" And Mid(mstr, 3, 1) <> "/" Then GoTo BadCalculation

If InStr(mstr, "/") <> 0 And Mid(mstr, 1, 1) <> "-" And Mid(mstr, 2, 1) <> "/" Then GoTo BadCalculation

If Mid(mstr, 3, 1) = "/" Then

numstr = Mid(mstr, 1, 2)

num = Val(numstr)

denstr = Mid(mstr, 4, (mstrlen - 2))

den = Val(denstr)

m = num / den

Else

End If

If Mid(mstr, 2, 1) = "/" Then

numstr = Mid(mstr, 1, 1)

num = Val(numstr)

denstr = Mid(mstr, 3, (mstrlen - 2))

den = Val(denstr)

m = num / den

Else

End If

If InStr(mstr, "/") = 0 Then

m = Val(mstr)

Else

End If

If m = 0 And mstr <> "0" Then GoTo BadCalculation

bstr = InputBox("Enter y intercept: i.e. -5/2, 10, 0.5")

bstrlen = Len(bstr)

If InStr(bstr, "/") <> 0 And Mid(bstr, 1, 1) = "-" And Mid(bstr, 3, 1) <> "/" Then GoTo BadCalculation

If InStr(bstr, "/") <> 0 And Mid(bstr, 1, 1) <> "-" And Mid(bstr, 2, 1) <> "/" Then GoTo BadCalculation

If Mid(bstr, 3, 1) = "/" Then

numstr = Mid(bstr, 1, 2)

num = Val(numstr)

denstr = Mid(bstr, 4, (bstrlen - 2))

den = Val(denstr)

b = num / den

Else

End If

If Mid(bstr, 2, 1) = "/" Then

numstr = Mid(bstr, 1, 1)

num = Val(numstr)

denstr = Mid(bstr, 3, (bstrlen - 2))

den = Val(denstr)

b = num / den

Else

End If

If InStr(bstr, "/") = 0 Then

b = Val(bstr)

Else

End If

If b = 0 And bstr <> "0" Then GoTo BadCalculation

If b > 0 Then

eqnstr = "y = " & mstr & "x + " & bstr

Else

End If

If b = 0 Then

eqnstr = "y = " & mstr & "x"

Else

End If

If b < 0 Then

eqnstr = "y = " & mstr & "x " & bstr

Else

End If

Range("$B$1").Select

ActiveCell.Value = eqnstr

x1 = -2

y1 = m * x1 + b

Range("$A$2").Select

ActiveCell.Value = x1

Range("$B$2").Select

ActiveCell.Value = y1

x2 = 0

y2 = b

Range("$A$3").Select

ActiveCell.Value = x2

Range("$B$3").Select

ActiveCell.Value = y2

x3 = 2

y3 = m * x3 + b

Range("$A$4").Select

ActiveCell.Value = x3

Range("$B$4").Select

ActiveCell.Value = y3

Range("$B$10").Select

ActiveSheet.Shapes.AddChart.Select

ActiveChart.ChartType = xlXYScatterSmoothNoMarkers

ActiveChart.SetSourceData Source:=Range("Sheet1!$A$2:$B$4")

ActiveChart.SeriesCollection(1).Name = "=Sheet1!$B$1"

ActiveSheet.ChartObjects(1).Name = "Chart 1"

Exit Sub

BadCalculation:

MsgBox "There was a problem with the calculation. Run program again"



When you paste the code in, it is likely there will be areas in red indicating errors in the transfer. They can be corrected (see pictures) by placing the mouse cursor exactly flush with the “dash” at line end and hitting “delete key”. The red type will become black, indicating it is now correct.

Close the program box and re-save the workbook. Hit “ctrl a” and the program will run. That’s it, hope it works well for you. One final note: Excel workbooks come in default mode with three sheets: "Sheet1", "Sheet2", "Sheet3". Might be a good idea to delete "Sheet2" and "Sheet3". Keep "Sheet1" as is, the program needs it.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article