Docsity

Excel VBA Macro Assignment Solutions - Prof. Kristen Hockman, Assignments of Accounting

Toggle sidebar
bg1
Name_________________________ Pawprint _______________ Section _____
Written Macro Assignment Solution
1. Open up Excel. Go to Cell B7 and name this cell “Pumpkin”. Then go to the Developer
tab on the Ribbon, and click on the Record Macro button in the Code group. Name your
macro “Referencing” and give it a shortcut key of “R”. After clicking OK to close the
Record Macro dialog box, do the following:
Select Cell A3 using absolute referencing (do not select the Use Relative
Reference button).
Using the drop-down arrow to the right of the Name Box, select “Pumpkin”.
Click on the Use Relative Reference button in the Code group on the
Developer tab to use relative referencing, and then select Cell C1.
Click on the Stop Recording button on the Code group on the Developer tab.
After you have performed these steps, view your code in Visual Basic Editor, and answer
the following questions:
a. What code was written when you selected Cell A3 using absolute referencing?
Range(“A3”).Select
b. What code was written when you selected the cell named “Pumpkin” using the
Name Box drop-down menu?
Application.Goto Reference:="Pumpkin"
c. What code was written when you selected Cell C1 using relative referencing?
ActiveCell.Offset(-6, 1).Range("A1").Select
Remember that ActiveCell.Select is the basic format used when writing macro
code using relative referencing. Also, please note that when writing out the VBA
code to move from one cell to another using relative referencing (as we did
above) the Range(“A1”) portion of the code is not necessary. It shows up
automatically (as displayed above) when we are recording a macro and moving
from one cell to the next using relative referencing, but if you are just writing out
the macro code, you do not need to include that portion of the code.
2. Cell B3 is the current cell you are in on your Excel spreadsheet. Write the appropriate
VBA code to perform the following:
a. Move from Cell B3 to Cell A9 using relative referencing
ActiveCell.Offset(6,-1).Select
Note that I did not include Range(“A1”) before .Select, as this is not necessary
(see my note above).
1
pf3
pf4
pf5

Partial preview of the text

Download Excel VBA Macro Assignment Solutions - Prof. Kristen Hockman and more Assignments Accounting in PDF only on Docsity!

Written Macro Assignment Solution

  1. Open up Excel. Go to Cell B7 and name this cell “Pumpkin”. Then go to the Developer tab on the Ribbon, and click on the Record Macro button in the Code group. Name your macro “Referencing” and give it a shortcut key of “R”. After clicking OK to close the Record Macro dialog box, do the following:  Select Cell A3 using absolute referencing (do not select the Use Relative Reference button).  Using the drop-down arrow to the right of the Name Box, select “Pumpkin”.  Click on the Use Relative Reference button in the Code group on the Developer tab to use relative referencing, and then select Cell C1.  Click on the Stop Recording button on the Code group on the Developer tab. After you have performed these steps, view your code in Visual Basic Editor, and answer the following questions: a. What code was written when you selected Cell A3 using absolute referencing? Range(“A3”).Select b. What code was written when you selected the cell named “Pumpkin” using the Name Box drop-down menu? Application.Goto Reference:="Pumpkin" c. What code was written when you selected Cell C1 using relative referencing? ActiveCell.Offset(-6, 1).Range("A1").Select Remember that ActiveCell.Select is the basic format used when writing macro code using relative referencing. Also, please note that when writing out the VBA code to move from one cell to another using relative referencing (as we did above) the Range(“A1”) portion of the code is not necessary. It shows up automatically (as displayed above) when we are recording a macro and moving from one cell to the next using relative referencing, but if you are just writing out the macro code, you do not need to include that portion of the code.
  2. Cell B3 is the current cell you are in on your Excel spreadsheet. Write the appropriate VBA code to perform the following: a. Move from Cell B3 to Cell A9 using relative referencing ActiveCell.Offset(6,-1).Select Note that I did not include Range(“A1”) before .Select , as this is not necessary (see my note above).

b. Move from Cell B3 to Cell D1 using relative referencing ActiveCell.Offset(-2,2).Select c. Move from Cell B3 to Cell E6 using absolute referencing Range(“E6”).Select

  1. When you click on the Insert button in the Controls group on the Developer tab, there are Form Controls and ActiveX Controls displayed to select from. When should you use the ActiveX Controls and when should you use the Form Controls? The ActiveX controls are macros that have already been written, which we can change properties of in order to work in a specific way for us. The ActiveX controls are used to manage specific events triggered when the control is used. We use the ActiveX controls to create things like spin buttons, scroll bars, option buttons, check boxes, and combo boxes. The Form Controls allow you to assign a button to a macro you created/wrote yourself, and it is designed to only work within Excel to run a macro.
  2. Define the following terms as they related to Visual Basic code: object, method, and property. Give an example of each. Object is an element of the Excel Application. Examples include a cell, a worksheet, or an entire workbook. Property is an attribute of an object that defines one of its characteristics. Examples include name, size, color, value, formula, or location. Method is an action that can be performed on an object. Examples include clear contents, copy, insert, delete, select, save, or close.
  3. Write the necessary code to define a variable in Visual Basic named Item to store text values. Dim Item As String
  4. Write the necessary code to define a variable in Visual Basic named Price to store values in dollar format. Dim Price As Currency
  5. Write the necessary code to define a variable in Visual Basic named Code to store whole number values. Dim Code As Integer
  1. Write the necessary VBA code to perform the following steps: a. Select Cell F9 using absolute referencing b. If the value in Cell F9 is less than 10, then the contents of Cell F9 should be cleared. Range(“F9”).Select If ActiveCell.Value < 10 Then Selection.ClearContents End If
  2. Write the necessary code to allow the user to perform the following calculation within Excel using a function named ERA : ERA = (EarnedRuns/InningsPitched)* Dim EarnedRuns As Single Dim InningsPitched As Single Function ERA (EarnedRuns, InningsPitched) ERA = (EarnedRuns / InningsPitched) * 9 End Function What type of procedure is this? Where would this code need to be written in Visual Basic Editor? This is a user-defined function. The code for user-defined functions should always be written in a module in Visual Basic Editor.
  3. Create a macro that will run when the file is opened. Set up a dialog box that will ask the user if they are graduating in May 2007. You should include a yes button and a no button in this dialog box, so the user can respond appropriately. If the user responds by clicking on the yes button, then you should display the message “Happy Graduation!” – just use an okay button here. If the user responds by clicking on the no button, then you should display a dialog box asking when they graduate. The user’s answer should be placed in Cell B7. Private Sub Workbook_Open() Dim Response As String Response = MsgBox(“Are you graduating in May 2007?” , vbYesNo) If Response = vbYes Then MsgBox(“Happy Graduation!”) Else Range (“B7”).Value = InputBox(“When will you graduate?”, “GRADUATION DATE”) End If End Sub

What type of procedure is this? Where should you write this macro in Visual Basic Editor so it will run properly? This is a private procedure (private sub/macro). The code for private procedures should always be written in This Workbook in Visual Basic Editor.

  1. You want to write a macro named Students from beginning to end to perform the following steps: a. Select Cell B5 using absolute referencing. b. If the value in Cell B5 is greater than 90, then “Senior” should be the value displayed in Cell C5. c. If the value in Cell B5 is greater than 60, then “Junior” should be the value displayed in Cell C5. d. If the value in Cell B5 is greater than 30, then “Sophomore” should be the value displayed in Cell C5. e. Otherwise, the value displayed in Cell C5 should be “Freshman”. f. Move down one cell below the active cell using relative referencing. g. Write this macro so steps b, c, d, e, and f will be performed over and over again until Excel comes to a blank cell (or while the active cell is not blank). Make sure the loop is written so the statements of code will be executed once before the condition is evaluated to determine if the code should be repeated. Sub Students( ) Range(“B5”).Select Do If ActiveCell.Value > 90 Then ActiveCell.Offset(0,1).Value=”Senior” ElseIf ActiveCell.Value > 60 Then ActiveCell.Offset(0,1).Value=”Junior” ElseIf ActiveCell.Value > 30 Then ActiveCell.Offset(0,1).Value=”Sophomore” Else ActiveCell.Offset(0,1).Value=”Freshman” End If ActiveCell.Offset(1,0).Select Loop Until ActiveCell.Value = “” End Sub (Alternatively, you could use Loop While ActiveCell.Value <> “”)
  1. You would like to create a scroll bar in Cell C3, which will allow the user to select a car loan amount ranging from $1,000 to $100,000. When you click on one of the arrows at either end of the scroll bar, you want the dollar amount to change by $100 increments. When you click between the arrows, you want the dollar amount to change by $1, increments. The loan amount selected by using the scroll bar should be displayed in cell B3. A B C 1 Car Loan Payment Calculator 2 3 Loan Amount $25, 4 Loan Term (in years) 5 6 Annual Interest Rate 6.75% 8 9 Monthly Payment ($492.09) 10 What properties need to be changed in order to create this scroll bar, and what should these properties be set at? Why would it not make sense to put a spin button in Cell C instead of a scroll bar? Linked Cell: B Max: 100000 Min: 1000 Small Change: 100 Large Change: 1000 A spin button should only be used when there are few possible values. When there are a lot of possible values, it is better to use a scroll bar. If you created a spin button for the car loan amount, and someone was purchasing a very expensive car, it would take a long time for them to get to the correct car loan amount by just clicking on an arrow. The scroll bar is much more efficient when there are a lot of possible values, because you can drag the scroll bar to go through the values very quickly.
  2. Now assume that you want to create a spin button in Cell C4 to allow the user to click on the arrows to change the loan term in Cell B4. The user should not be allowed to enter anything less than one or anything greater than 5 for the loan term. What properties need to be changed in order to create this spin button, and what should these properties be set at? Linked Cell: B Max: 5 Min: 1