Solutions to Problems in Chapter 1

Total Page:16

File Type:pdf, Size:1020Kb

Solutions to Problems in Chapter 1

Chapter 10 Intro to Visual Basic

Selected Solutions to Problems in Chapter 10

Problem 1

Using Excel’s macro-recorder, record the VB code for formatting a cell to Times New Roman font, bold type, and the color red.

The following VB code represent one method for formatting a cell to Times New Roman, bold type, and the color red.

Sub FormatCell() ' ' FormatCell Macro ' Macro recorded 3/20/2003 by Samantha Taylor '

' With Selection.Font .Name = "Times New Roman" .Size = 10 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Selection.Font.Bold = True With Selection.Interior .ColorIndex = 3 .Pattern = xlSolid .PatternColorIndex = xlAutomatic End With End Sub

SG10-1 Chapter 10 Intro to Visual Basic

Problem 2

In a new Excel spreadsheet, type 10 in row 1 column A cell location and 10 in row 2 column A cell location. Using Excel’s macro-recorder, record the VB code for adding the cells together in row 3 column A cell location.

The following VB code represent one method for adding two numbers in cell locations row 1 column A and row 2 column A.

Sub AddCells() ' ' AddCells Macro ' Macro recorded 3/20/2003 by Samantha Taylor '

' ActiveCell.FormulaR1C1="=R[-2]C+R[1]C" Range("A4").Select End Sub

SG10-2 Chapter 10 Intro to Visual Basic

Problem 3

In a new Excel spreadsheet, type 10 in row 1 column A cell location and 10 in row 1 column B cell location. Using Excel’s macro-recorder, record the VB code for multiplying the cells together in row 1 column C cell location.

The following VB code represent one method for multiplying two numbers in cell locations row 1 column A and row 1 column B.

Sub MultiplyCells() ' ' MultiplyCells Macro ' Macro recorded 3/20/2003 by Samantha Taylor '

' ActiveCell.FormulaR1C1="=RC[-2]*RC[-1]" Range("C2").Select End Sub

SG10-3 Chapter 10 Intro to Visual Basic

Problem 4

Rewrite the code in Chapter 12 Figure 12.12 so as to multiply the two numbers together instead of adding them.

The following VB code represent one method for rewriting the code in Figure 12.12 so as to multiply the two numbers together instead of adding them.

Dim intFirstNumber As Integer Dim intSecondNumber As Integer Dim MultResult2 As Integer

Sub MultResult()

' ' CallMultResult Macro ' Macro created {date} by {name} '

intFirstNumber = InputBox(Prompt:="Type the first integer.") intSecondNumber = InputBox(Prompt:="Type the second integer.")

Call MultResult1

MsgBox intFirstNumber & " * " & intSecondNumber & " = " & MultResult2 & "."

End Sub

Sub Multesult1()

MultResult2 = intFirstNumber * intSecondNumber

End Sub

SG10-4 Chapter 10 Intro to Visual Basic

Problem 5

Create VB code that when given an ordered pair, (x1, x2) and (x3, x4), will construct a slope-intercept equation y = mx + b, find and list the ordered pairs, and then graph the equation on any form.

This code, when given an ordered pair, is one method for constructing a slope-intercept 'equation (y=mx + b), finding and listing (in any listbox) the ordered pairs, and then graphing the equation on any form.

'Declarations. X1, X2, Y1, Y2 are the variables 'that hold the X and Y values for the equation's 'two ordered pairs. 'The coefficient variable holds the coefficient 'of the x variable in a slope-intercept form of 'the line's equation. 'Equation holds the actual equation. 'CatX(5) and CatY(5) hold the ordered pairs when 'the equation is evaluated. 'Counter is used to drive the loop that will evaluate 'the equation. Dim Y1 As Integer Dim Y2 As Integer Dim X1 As Integer Dim X2 As Integer Dim YInt Dim Coefficient As Integer Dim Equation As String Dim Standard As String Dim CatX(5) Dim CatY(5) Dim Counter As Integer Public Function Evaluate()

'Ask the user what the values of the Y1 'Y2, X1, X2 are.

Y1 = InputBox("Please enter the Y value of the first point of the line.", "Enter Y1") Y2 = InputBox("Please enter the Y value of the first point of the line.", "Enter Y2")

X1 = InputBox("Please enter the X value of the first point of the line.", "Enter X1") X2 = InputBox("Please enter the X value of the first point of the line.", "Enter X2")

'Find the slope of the line using the standard 'formula: Y1-Y2 divided by X1-X2.

Coefficient = ((Y1 - Y2) * (-1)) / ((X1 - X2) * (-1))

'Find the y-intercept of the equation by subtracting x 'and its coefficient from the value of y (leaving b, or 'the y-intercept variable on the right side of the equation.

YInt = (Y1 - (X1 * Coefficient))

SG10-5 Chapter 10 Intro to Visual Basic

'Set the equation to: y = mx + b or y = slope * x + y-intercept.

Equation = "The equation is: " Equation = Equation & vbNewLine Equation = Equation & "y = " & Coefficient & "x + " & "(" & YInt & ")"

'tell the user the equation using a messagebox.

MsgBox Equation End Function

Public Function Graph(ByVal DrawForm As Form)

'Draw a line of the ordered pairs starting from 'the lowest value of x (0) and ending with the 'highest value of x (5).

DrawForm.Line (CatX(0), CatY(0))-(CatX(5), CatY(5)) End Function

Public Function GetPoints(ByVal Storage As ListBox)

'Declare a buffer variabel to hold things.

Dim StorageVariable

'Start the loop that will begin evaluating the 'equation.

For Counter = 1 To 5 Step 1

'Set the first array space of CatX to the current 'value of counter. This will act as storage for 'the ordered pairs of (0, ..), (1, ..), etc.

CatX(Counter) = Counter

'Set the value of Y AFTER setting the variable X 'in the equation to the current value of counter.

CatY(Counter) = ((CatX(Counter) * Coefficient) + YInt)

'Store the ordered pair in our buffer variable

StorageVariable = CatX(Counter) & "," & CatY(Counter)

'Add the ordered pair to our listbox (specified by ByVal Storage as Listbox) 'Then, add a "----" to separate between ordered pairs of different equations.

Storage.AddItem StorageVariable Storage.AddItem "----"

'Repeat everything in a loop.

Next Counter

SG10-6 Chapter 10 Intro to Visual Basic

End Function

SG10-7

Recommended publications