Sie sind auf Seite 1von 3

VBA Session 1 tasks

The practical tasks this week are concerned with writing macros from scratch rather than recording (Recording code can still be used to find out about the code to be used). You should aim to produce a final product that is efficient (no extraneous code) and clearly commented. Click Macros on the Developer ribbon, add a name and click on Create.

Practical session 1
Task 1.1
This task is concerned with copying cells using a range of code Open a new workbook and insert some data in the cells A1:B10. a) Using Define name on the Formulas tab define the range of cells A1:B10 as ChosenCells or a name of your choice. Create a new Macro that copies the contents of your chosen cells as follows . Sub Copy1() Range(ChosenCells).Select Selection.Copy ActiveCell.Offset(3,7).Select ActiveSheet.Paste End Sub Run the Sub and note the result. b) In the VBE (Alt F11) copy and paste the Sub from a) renaming the Sub and editing so that the code is copied to the cells starting 3 cells to the right and 5 cells down from the ActiveCell. c) Start a new Macro from scratch to Select the cells A1:B3 and copy to the cells starting from C2 Sub Copy3() Range("A1.B3").Select Selection.Copy Range("C2").Select ActiveSheet.Paste End Sub d) Copy, rename and edit the sub from c) so that the cells A4:B10 are copied to somewhere else on the page

e) Start a new Macro from scratch to: Select the cells A1:B3 relative to the ActiveCell and copy to the cells starting from C2 Sub Copy5() ActiveCell.Range("A1:B3").Select Selection.Copy ActiveCell.Range("C2").Select ActiveSheet.Paste End Sub f) Copy, rename and edit the sub from e) so that the cells A4:B10 are copied to somewhere else on the page

Hint: You will need to ensure that you have contents in the cells to copy!

Task 1.2
This task is concerned with using variables a) Create a new Macro which defines the variable Tomorrow and outputs the variable to cell B2. Tomorrow = Now()+1 Range(B2).Value=Tomorrow b) Copy and paste the sub from a) to create a new sub where a variable for the day after tomorrow is output in the cell Offset(1,2) from the Active cell ActiveCell.Offset(1,2).Value = NextDay c) Create a sub where a variable for tomorrow is output to the cell three cells above and two cells to the left of the active cell. d) Find out what happens if your offset takes you off the worksheet e.g. Offset(-4, -3) from cell B2.

Task 1.3
This task is concerned with using variables a) Using the ActiveCell Object write a sub that produces starting from the selected cell using the function Now() Yesterday Today Tomorrow 01/12/2011 07:44 30/11/2011 07:44 02/12/2011 07:44

b) Copy and paste the sub from a) and edit to achieve different effects including formatting the text and cell shading c) From the Formulas tab, click on Date&Time to view different functions. Experiment with these, creating different tables of date and Time data. NB not all these will work in a straight forward way

Practical session 2
Task 2.1
You will need a new worksheet that has a selection of data (as numbers) in a range of cells. Create a macro to define a variable for the maximum value in the data set and output the variable as a message box. Dim TheMax as Double TheMax =WorksheetFunction.Max(Range(YourNameForYourRange)) MsgBox "The Maximum is " & TheMax Double is a data type. You will need to define your range first b) Create further macros to output the minimum, the sum, the average (mean) and the product of your numbers. Edit the output on the Message Box as appropriate.

Task 2.2
a) Create a macro to define a function to calculate the product of three numbers. Function x3Numbers(num1, num2, num3) x3Numbers = num1 * num2 * num3 End Function In a cell on your worksheet type = x3 Your function will appear for you to select (double click) so you dont have to type the whole name. You will need to enter (click on) three cells to enter the 3 arguments of the function and place commas between the arguments. b) Create a macro to define a function that divides one number by the sum of two others. Use your function on the worksheet

Task 2.3
a) Create a macro to define a sub: Sub ShowResult() Dim intN1 As Integer, intN2 As Integer, intN3 As Integer Dim Result As Integer intN1 = 2 intN2 = 3 intN3 = 5 Product = x3Numbers(intN1, intN2, intN3) MsgBox The product is& Product End Sub b) Edit the macro so that the inputs are in cells A1 to A3. Tip: intN1=Range(A1).Value Create a button to run the macro Developer tab/ Insert/Form Controls/ Button etc. c) Experiment with different formula e.g. those you have used for forecasting

Das könnte Ihnen auch gefallen