250 likes | 384 Views
Visual Basic for Applications in Microsoft Excel (1). Week 6. Week 6. Assessment 2 Do Loops Custom Functions. Sub SheetCounter () MsgBox ("There are " & Worksheets.Count & " worksheets in this _ workbook") End Sub Sub CreateStaffList () Dim r As Worksheet Dim s As Worksheet
E N D
Week 6 Assessment 2 Do Loops Custom Functions
Sub SheetCounter() MsgBox ("There are " & Worksheets.Count & " worksheets in this _ workbook") End Sub Sub CreateStaffList() Dim r As Worksheet Dim s As Worksheet Worksheets.Add.Name = "StaffList" Set r = Worksheets("net_pay") Set s = Worksheets("StaffList") r.Range("G2:G10").Copy s.Range("A1:A10") End Sub
Assessment 2 Produce a macro that, if run on a new workbook will: • Ask the user how many stores and how many products they wish to monitor • Create new worksheets so that there are enough for one for each week (4?) and a summary sheet • Name each work sheet appropriately • Ensure each sheet is set up ready for the user to input data , with formulas to calculate the row and column totals
Extensions • Add formulas in the summary sheet to “pull through” the data from the previous sheets • Extend the summary sheet to hold other useful calculations (eg. best performing store / product) • Allow the user to input the number of weeks the workbook will be used for • Ask the user which month/ year the workbook is intended for and save the workbook using month/year as a name
Do loops • There are four varieties of the Do loop: • Do While . . . Loop • Do . . . Loop While • Do Until . . . Loop • Do . . . Loop Until
Do While • (Note: Ctrl + Break should terminate most runaway loops; if not Ctl Alt + Delete and end Excel - and lose unsaved work!)
Example of Do While Sub MarkEmptyCells() Do While IsEmpty(ActiveCell) With ActiveCell .Value = "This cell is blank" .Font.Bold = True .Offset(1, 0).Activate End With Loop MsgBox "We are out of the loop. That's all folks" End Sub
Example of Do Until Sub markEmptyCells_until() Do Until not IsEmpty(ActiveCell) ActiveCell.Value = "Default value" ' whatever ActiveCell.Font.Bold = True 'makes it easy to see ActiveCell.Offset(1, 0).Select Loop MsgBox "We are out of the loop. That's all folks" End Sub
Points to Note: • All the following programs select the first empty cell in the same column below the active cell, four illustrating the use of Do loops; the fifth uses the statement that is recorded when you press Ctrl and the Down Arrow key
Points to note: Loops can contain control statements such as If Then or other loops If statements can contain loops You can break out of a loop if necessary by using the Exit Do keyword
Do Loop Exercises Do Loop Leap Years (part 1)
Creating Custom Functions function functionname() -- function code goes here -- end function You are probably familiar with using built-in functions like SUM, AVERAGE and IF. With VBA, you can create your own functions, uniquely tailored to your needs.
Analysis of a NetPay Function Let's say you want to create a function that calculates how much your Net Pay is after deductions. The function would involve these values: GrossPay: how much you earn before deductions Income Tax as a percentage National Insurance as a percentage Pension Fund as a percentage
Analysis of a NetPay Function NetPay is how much you take home after IncomeTax, National Insurance and Pension Fund contributions have been deducted. The maths would look like this: Deductions = (GrossPay * IncomeTax) + (GrossPay * NI) + (GrossPay * Pension) NetPay = GrossPay - Deductions
Analysis of a NetPay Function Function NetPay _ (GrossPay As Double, IncomeTax _ As Double, NI As Double, Pension As_ Double) As Double Dim Deductions As Double Deductions = (GrossPay * IncomeTax) +_ (GrossPay * NI) +(GrossPay * Pension) NetPay = GrossPay - deductions End Function
Using the function You can use functions in the same way as you use Excel functions. Your VBA functions will appear in the “User Defined Functions” category
Another Example A function called myFV, (so it doesn’t conflict with the inbuilt Excel function FV) which works out the value of an investment after compound interest has been added, i.e. its future value. Function myFV(pr As Single, rate As Single, nper As Integer) As_ Single myFV = pr * (1 + rate) ^ nper End Function On the other hand you might also want a function that works out just the compound interest i.e. minus the principal. Function Compound(pr As Single, rate As Single, nper As Integer) as Single Dim fv As Single fv = (pr * (1 + rate) ^ nper) compound = fv - pr End Function
Calling the Insert Function dialog You can create a macro to call the FunctionWizard or Insert Function dialog – which would save a few seconds each time you run it: ActiveCell.FunctionWizard You would then choose which function to use …
WorksheetFunction method You can call functions with the following code: Application.WorksheetFunction.FunctionName(arg1,arg2)
Next Week Error Handling Conversion Functions