Docsity

Macro Assignment Solutions and Coding Exercises - Prof. Kristen Hockman, Study notes of Accounting

Toggle sidebar
bg1
Name_________________________ Pawprint _______________ Section _____
Written Macro Assignment Solutions
True/False Questions. Read each statement carefully, and neatly write the word TRUE on the
line next to the statement if you believe the statement is true, or neatly write the word FALSE on
the line next to the statement if you believe the statement is false.
FALSE 1. Do…Loops are used in macros to repeat lines of code a specified
number of times.
TRUE 2. The area where macro code is displayed in Visual Basic is called the
Code Window.
TRUE 3. A macro name is not required when creating a private macro.
TRUE 4. When declaring a variable, Dim stands for “define in memory”.
FALSE 5. Objects, Variables, and Actions are used to write macro code.
TRUE 6. One way to create a new macro is to use Record New Macro.
TRUE 7. The If…Then…Else…End If code allows code to be conditionally
executed depending on whether a specified logical condition has been
met.
FALSE 8. The last line of code for a private macro should be End Private Sub.
FALSE 9. The formula for a user-defined function must be entered in quotation
marks.
TRUE 10. A public macro/procedure is activated by the user pressing the
shortcut keys or clicking on a button.
1
pf3
pf4
pf5

Partial preview of the text

Download Macro Assignment Solutions and Coding Exercises - Prof. Kristen Hockman and more Study notes Accounting in PDF only on Docsity!

Written Macro Assignment Solutions

True/False Questions. Read each statement carefully, and neatly write the word TRUE on the line next to the statement if you believe the statement is true, or neatly write the word FALSE on the line next to the statement if you believe the statement is false. FALSE 1. Do…Loops are used in macros to repeat lines of code a specified number of times. TRUE 2. The area where macro code is displayed in Visual Basic is called the Code Window. TRUE 3. A macro name is not required when creating a private macro. TRUE 4. When declaring a variable, Dim stands for “define in memory”. FALSE 5. Objects, Variables, and Actions are used to write macro code. TRUE 6. One way to create a new macro is to use Record New Macro. TRUE 7. The If…Then…Else…End If code allows code to be conditionally executed depending on whether a specified logical condition has been met. FALSE 8. The last line of code for a private macro should be End Private Sub. FALSE 9. The formula for a user-defined function must be entered in quotation marks. TRUE 10. A public macro/procedure is activated by the user pressing the shortcut keys or clicking on a button.

Problems. Read each problem carefully and follow the instructions given.

  1. Open up Excel. Go to Cell E9 and use the Name Box to name this cell “Winter”. 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 C7 using absolute referencing (do not select the Use Relative Reference button).  Using the drop-down arrow to the right of the Name Box, select “Winter”.  Click on the Use Relative Reference button in the Code group on the Developer tab to use relative referencing, and then select Cell A.  Click on Stop Recording button in 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 C7 using absolute referencing? Range(“C7”).Select b. What code was written when you selected the cell named “Winter” using the Name Box drop-down menu? Application.Goto Reference:="Winter" c. What code was written when you selected Cell A4 using relative referencing? ActiveCell.Offset(-5, -4).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. Assume 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 E8 using relative referencing ActiveCell.Offset(5,3).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 A1 using relative referencing
  1. Write the necessary code to allow the user to perform the following calculation within Excel using a function named NetCarbs : NetCarbs = TotalCarbs - Fiber Dim TotalCarbs As Integer Dim Fiber As Integer Function NetCarbs (TotalCarbs, Fiber) NetCarbs = TotalCarbs-Fiber 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.
  2. Create a macro that will run when the file is opened. Set up a dialog box that will ask the user if they are traveling for Thanksgiving. 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 No button, then you should display the message Enjoy Thanksgiving Break! – provide just an OK button for the user to click on to close the dialog box. If the user responds by clicking on the Yes button, then you should display a dialog box asking where they will be going. The user’s answer should be placed in Cell A4. Give this dialog box the title Thanksgiving. Then display the message Enjoy Thanksgiving Break! , and provide just an OK button for the user to click on to close the dialog box. Private Sub Workbook_Open() Dim Response As String Response = MsgBox(“Are you traveling for Thanksgiving?” , vbYesNo) If Response = vbNo Then MsgBox(“Enjoy Thanksgiving Break!”,vbOKOnly) Else Range (“A4”).Value = InputBox(“Where are you going?”,”Thanksgiving”) MsgBox(“Enjoy Thanksgiving Break!”,vbOKOnly) End If End Sub
  1. The partial worksheet shown below has been developed for course registration at a local community college. When the course number is placed in a cell in the A column, the other columns will display the appropriate information (the information is obtained by using VLOOKUP functions in the other columns). 1 2 3 4 5 6 7 8 A B C D E F G COURSE NUMBER DESCRIPTION DAYS TIME LOCATION INSTRUCTOR CREDITS Using the blank space provided on the next page, write the appropriate code to do the following: a. Create a macro named Registration. b. Use the Name Box to select the cell that has been named COURSE (Cell A2). c. Create one dialog box that will ask the user to enter the number of the course they wish to register for. Your code should place the user’s response in the appropriate cell in the A column – A2 for the first course, A3 for the second course, etc. (HINT: use relative referencing.). The title for this dialog box should be Course Number. Write the code for this dialog box, so it will be repeated if the course number entered is not between 9999 and 50001. Note that the description, days, time, location, instructor and credits will automatically appear based on the course number the user enters in the A column. d. Loop 8 times so that each time a new course is entered, the course number is placed in column A on the next row down, allowing the user to enter up to 8 courses. e. Assume that the total number of credit hours appears in Cell G9. If that total is 6 hours or less, create a message box telling the user their tuition is $3,000. If that total is between 6 and 12 hours, create a message box that tells the user their tuition is $5,000. If the total is 12 hours or more, your message should tell the user their tuition is $7,000.