Solutions to Problems in Chapter 1

Solutions to Problems in Chapter 1

<p> Chapter 10 Intro to Visual Basic</p><p>Selected Solutions to Problems in Chapter 10</p><p>Problem 1</p><p>Using Excel’s macro-recorder, record the VB code for formatting a cell to Times New Roman font, bold type, and the color red.</p><p>The following VB code represent one method for formatting a cell to Times New Roman, bold type, and the color red.</p><p>Sub FormatCell() ' ' FormatCell Macro ' Macro recorded 3/20/2003 by Samantha Taylor '</p><p>' 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</p><p>SG10-1 Chapter 10 Intro to Visual Basic</p><p>Problem 2</p><p>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.</p><p>The following VB code represent one method for adding two numbers in cell locations row 1 column A and row 2 column A.</p><p>Sub AddCells() ' ' AddCells Macro ' Macro recorded 3/20/2003 by Samantha Taylor '</p><p>' ActiveCell.FormulaR1C1="=R[-2]C+R[1]C" Range("A4").Select End Sub</p><p>SG10-2 Chapter 10 Intro to Visual Basic</p><p>Problem 3</p><p>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.</p><p>The following VB code represent one method for multiplying two numbers in cell locations row 1 column A and row 1 column B.</p><p>Sub MultiplyCells() ' ' MultiplyCells Macro ' Macro recorded 3/20/2003 by Samantha Taylor '</p><p>' ActiveCell.FormulaR1C1="=RC[-2]*RC[-1]" Range("C2").Select End Sub</p><p>SG10-3 Chapter 10 Intro to Visual Basic</p><p>Problem 4</p><p>Rewrite the code in Chapter 12 Figure 12.12 so as to multiply the two numbers together instead of adding them.</p><p>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.</p><p>Dim intFirstNumber As Integer Dim intSecondNumber As Integer Dim MultResult2 As Integer</p><p>Sub MultResult()</p><p>' ' CallMultResult Macro ' Macro created {date} by {name} '</p><p> intFirstNumber = InputBox(Prompt:="Type the first integer.") intSecondNumber = InputBox(Prompt:="Type the second integer.")</p><p>Call MultResult1</p><p>MsgBox intFirstNumber & " * " & intSecondNumber & " = " & MultResult2 & "."</p><p>End Sub</p><p>Sub Multesult1()</p><p>MultResult2 = intFirstNumber * intSecondNumber</p><p>End Sub</p><p>SG10-4 Chapter 10 Intro to Visual Basic</p><p>Problem 5</p><p>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.</p><p>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.</p><p>'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()</p><p>'Ask the user what the values of the Y1 'Y2, X1, X2 are.</p><p>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")</p><p>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")</p><p>'Find the slope of the line using the standard 'formula: Y1-Y2 divided by X1-X2.</p><p>Coefficient = ((Y1 - Y2) * (-1)) / ((X1 - X2) * (-1))</p><p>'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.</p><p>YInt = (Y1 - (X1 * Coefficient))</p><p>SG10-5 Chapter 10 Intro to Visual Basic</p><p>'Set the equation to: y = mx + b or y = slope * x + y-intercept.</p><p>Equation = "The equation is: " Equation = Equation & vbNewLine Equation = Equation & "y = " & Coefficient & "x + " & "(" & YInt & ")"</p><p>'tell the user the equation using a messagebox.</p><p>MsgBox Equation End Function</p><p>Public Function Graph(ByVal DrawForm As Form)</p><p>'Draw a line of the ordered pairs starting from 'the lowest value of x (0) and ending with the 'highest value of x (5).</p><p>DrawForm.Line (CatX(0), CatY(0))-(CatX(5), CatY(5)) End Function</p><p>Public Function GetPoints(ByVal Storage As ListBox)</p><p>'Declare a buffer variabel to hold things.</p><p>Dim StorageVariable</p><p>'Start the loop that will begin evaluating the 'equation.</p><p>For Counter = 1 To 5 Step 1</p><p>'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.</p><p>CatX(Counter) = Counter</p><p>'Set the value of Y AFTER setting the variable X 'in the equation to the current value of counter.</p><p>CatY(Counter) = ((CatX(Counter) * Coefficient) + YInt)</p><p>'Store the ordered pair in our buffer variable</p><p>StorageVariable = CatX(Counter) & "," & CatY(Counter)</p><p>'Add the ordered pair to our listbox (specified by ByVal Storage as Listbox) 'Then, add a "----" to separate between ordered pairs of different equations.</p><p>Storage.AddItem StorageVariable Storage.AddItem "----"</p><p>'Repeat everything in a loop.</p><p>Next Counter</p><p>SG10-6 Chapter 10 Intro to Visual Basic</p><p>End Function</p><p>SG10-7</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us