You are on page 1of 12
36 Learn Microsoft Excel — Advanced Level Click on Insert, Module and enter this macro code: Sub Auto_open() Load Details Details. Show End Sub This code sets up a macro called Details to enter the customers details onto a spreadsheet. A macro has been recorded that will transfer the data from a user form co the spreadsheet. After completing your user form save the spreadsheet. When saving you are likely to be prompted by a Visual Basic message telling you to click NO in order to save to an Excel macro enabled workbook. Do this or your user form or any macro you may write will not be saved. Now open back up your spreadsheet. First off all we would like the Customers surnames to be put into alphabetical order in our spreadsheet. Return to the spreadsheet by clicking on the Microsoft Excel icon. Highlight columns A to F, click Insert, Name, Define and name it Namefile. Or in the name box name it Namefile, Now we will record the macro. Click Tools, Macros, Record new macro and name it Addnames, and click OK or View, Macros and Record macro. Select row 2, Right click on mouse and click Insert row. Now load the macro Auto_open, by clicking Tools, or View, Mactos. We recorded this macro by inserting a module. It will show the Details macro. Ic is called Auto_open, so it opens up with the spreadsheet, once macros have been enabled. Learn Microsoft Excel — Advanced Level 37 In the user form enter the following data: In the Title box enter, Mr. In the Forename box enter, Peter. In the surname box enter, Wainwright. In the Address Box enter, 23 Forest Street. In the town box enter, Blackpool and in the Postcode box enter BES 4RD. After entering Data close the data form by choosing the Red cross button. Click on Namefile in the names box. Sort Data by Surname. Click on Data, Sort and check my list has header row, so header rows dont get sorted as well, Click OK, and Stop recording, then place cursor back on A2. Return to your spreadsheet and add a Command button to your spreadsheet, and edit it Return to Data Form. To get your form controls/active X controls up click on View, Toolbars, Control toolbox or go into Developer, Insert and choose a Command button. View code of Command button and Assign macro Addnames to it, by placing the macro name after Command button1_click() Private Sub Commandbutton1_click() Addnames End Sub Learn Microsoft Excel — Advanced Level 4] Form controls & ActiveX controls are very similar, in that they appear to do almost identical functions. The form controls are much older and straightforward. The ActiveX controls are more advanced and powerful and enable the user to do more with their form controls within Visual Basic. I would not use ActiveX controls; Combo box, Listbox, Scroll Bars and Text Boxes for this exercise. [ have found them to be buggier, than form controls and not as straightforward as you would expect. If you are to use them you would have to use nested IF formula which is covered in this book to find your multiple values and the statements would be very long. The text written is for the Old form controls and not Activex controls. If you were applying ActiveX controls, when formatting your control substitute Input range box for List fill range & Cell Link for Linked Cell. To Edit Option buttons or Command buttons you would have to use Caption in the properties window. After using Form controls, you may want to experiment using ActiveX controls and see if you can use Nested IF formula to find your multipliers. For IG number in G8 you would use the formula =IF(G6 ="Seat Ibiza’, 13,[F (G6 =" Saab 9-3 Bio”, 12,1F(G6="Suzuki Swift’8))) and so on until all makes of car are noted. The number of brackets at the end of the formula depends on how many statements are given. In D11 and D12, we will need co add Option buttons to determine the sex of the customer. Bring up the toolbox again. Click on View, Toolbars, Control toolbox or Developer, Insert and click on the Option button icon. Drag out che option button and place it in D11. Adjust the option button size, so it fits nicely, Now right click on your mouse and a menu will come up, Click on Format control and in Cell link put E12, 42 Learn Microsoft Excel — Advanced Level Also have the check button ticked if not already done. Whichever option is checked we need to refer this option to another cell which will give a value as either 1 or TRUE being Male or 2 or FALSE being Female. In this case E12 is used. Add another option button for Female in D12. Format control it and put the cell link as E12 again. Ifyou have chosen to use ActiveX option buttons these would return the values TRUE & FALSE. Don’t assign a control source to your second option button. If using old form controls, make an amendment to your Multipliers!worksheet. Click on it and were you see the values TRUE & FALSE in the named table;Sex(A15:C16) change the values to 1 & 2, so Excel can look up these values. Use the appropiate formulae as specified in the formulae sheet on page 44, In D14 & D16 we are going to use Combo boxes, so the imputer can select an option froma mass of grouped data. Using the control toolbox again and clicking on the Combo box icon place your first Combo box in D14 adjusting its size accordingly. Now right click on your mouse and click on Format control. In the input range box type = Groups!Cars and in the cells link box type =E14. The Combo box information has been found from the Groups worksheet and the named cells Cars. Excel has found this information from the cells you have told it to look for in the Groups! Worksheet. When asking Excel to look for another sheet like Groups always remember to use the exclamation mark! The cell link E14, will reveal a number, This number will be relevant for the other half of the quote, when we ask excel to look up the make of car. Learn Microsoft Excel — Advanced Level 43 Place another Combo box in D16, right click on mouse and select Format control. In the Input range type =MultiplierstAgegroups. and Cell link as E16. The cell link E16, will reveal a number. This number will be relevant for the other half of the quote, when we ask Excel to look up the multiplier for that age range. A number for Excel to refer to is not available when using ActiveX controls. Instead different nested IF Formula would have to be used or Visual Basic Language. Add List boxes in D18 & D20 using the control tool box for the Areaand Type fields. When using List boxes we need to find the Worksheet also in our formulae. For Area type the Input range as =Multipliers!Risk and Cell link as E18, Check the selection type as single, For Type, type the Input range as =Multipliers!Insurance, Check the selection type as single and authorize the Cell link as E20. In D22 add a Checkbox and place the cursor inside the Checkbox and name it Extra Driver. Format control it, by having the checked box ticked and the Cell link as E22. A checked box will reveal a True value in E22. Both kinds of control use the same formulae. As you will note from our Multipliers worksheet che multiplier for having an Extra Driver on the Insurance Quote is 1.7. For determining how many years No claims bonus a driver has we will add a Spinner to the Quote worksheet. Format control chis as: Link to D24 Max =4 Min =0 Inc = 1 or (Small change) ‘The Spinner will now include a maximum of 4 years no claims bonus, and is incremented by 1 year each time. When placing your Spinner Learn Microsoft Excel — Advanced Level 45 ‘The data for finding the make of car in G6 looks up the value in E14, which returned a number. The second part tells Excel to look this number up in the named table Data and gives the value of this from column 2. As you can see I have started off with an IF function. Lets look at the formulae for the make of car. It starts off =IF(E14="""”, I didn’t necessarily have to have included this, the spreadsheet would still find the values if | use the Vlookup function on its own =vlookup(E14,Data,2)) Lhave used the IF function with apostrophes “’”” to get rid of errors when data entry returns a blank, otherwise the formula for looking up data can start off with =vlookup. The IF formula = IF(D6=""3" in the Totals columns will not show the quote figures, if the Surname in D6 is not entered. [fit is entered the cells can be calculated as shown. The sex of the driver formula in G9 reads =LEFT(VLOOKUP(E12,Sex,2))) A left Viookup will return the first initial of data to be found in column 2, from the named table; Sex, onto the Quote sheet. E12 is the value to be looked up in the Multipliers! sheet between the range of A15:C16 and will recurn the value in column 2 as an initial. If this is Male then the initial will be returned as M. The extra driver formula in G12 reads = IF(E22=TRUE;’Y",N”). This statement looks up the value given in E22. If it states TRUE, the value given in G12 will read “Y”, otherwise “N”. ‘That completes the Quotes worksheet. Give yourself some time to study the formulae to get a better understanding of how they work. Learn Microsoft Excel — Advanced Level 47 Tools, Macros, Stop recording, To file the quote, we need to add. a Command button. Click on View, Toolbars, Forms and drag out a Command button or click Developer, Insert, Form Control and choose a Command button. Place it in a suitable position on your Quote worksheet. Name it FileQuote. Format it by right clicking mouse to bring up the Format menu, Assign macro and choose FileQuote. To clear the screen for a new quote record anew macro called Clearscreen, Switch to Quotes worksheet. Highlight cells D6 to D10. Edit, Clear, All. Highlight cells E12:E22, Edit, Clear Contents. Highlight cell D24 and Edit, Clear Contents. Return cursor to D6, and Stop recording. Add a Command button and name it Clearscreen. Format it by right clicking mouse to bring up the Format menu, Assign macro and choose Clearscreen. To view the Customers worksheet, record a new macro called ViewQuotes. Click on Customers worksheet and Stop recording, Add a Command button on the Quotes worksheet, name it View Quotes and assign the macro. At the moment we have three Command buttons; Clearscreen, Filequote and Viewquotes on our system interface. You may want to add more buttons, such as Printquote or Exit application. With the use of Format control, you can transform the look and 48 Learn Microsoft Excel — Advanced Level feel of your system interface, by including your company logo and making use of Fonts, Colour, Fill, Lines and Borders. It may be more appropriate to have a separate worksheet for printing a quote. You would need to link formula from the Quotes worksheet to your new worksheet, named Printquote. To date your quote, the formula you would use is =TODAY() To valid a quote for 28 days for example, the formula you would use is =TODAY()+28 The last part of the system interface is to add an Auto_open macro, so the system will load the spreadsheet without Gridlines, Sheet tabs, Rows and Column headings followed by recording an Auto_ close macro to restore Excels original settings when closing the application down. Record amacro named Auto_open Click on Tools, Options and Uncheck the Gridlines, Sheet tabs, Row and Column headings, Scroll bars, Formula bar and Status bar and click on OK or go into View, Show/hide and Uncheck features. Click on View, Toolbars and remove Standard and Formatting toolbars if appropriate and Stop recording. Record a macro named by restoring what you have done in the auto_open macro. The last stage of the Quotation system application would be to adda Front End. This is the initial page of your system interface and will include Command buttons to direct you to your spreadsheet. Learn Microsoft Excel — Advanced Level 49 Click on your Visual Basic editor (ALT and F11) and Insert a user form, Design it and using the properties window set your Caption, Height and Width. Increase the zoom setting to make the user form larger and add three Command buttons, and name them Exit, Enter and Details. View the code of the Command buttons, and add additional code between the Sub and End Sub. ‘The Exit button will close down the application. Private Sub Commandbutton1_click() Application. Quit End Sub The Enter button will hide the user form and run a macro called start. Private Sub Commandbutton2_click() Userform1.Hide Start End Sub Iris necessary to make changes to the Auto_open macro we recorded now as we would want to remove the user form when the Enter button is pressed and let it run the original auto_open macro, so let’s view the auto_open macro code and add the following lines after macro recorded: Sub Auto_open() Load userform1 Userform1.show End Sub Sub start() 50 Learn Microsoft Excel — Advanced Level Once additional code is added, close the macro and view Command buttons code again. The final Command button the Details button is entirely up to you what you want to include. Visual Basic has many message box settings and icons. For now type in the following code, this will run a Message box: Private Sub Commandbutton 3_click() Msgbox “Any Queries about this application contact Pe- ter on 01254 666333", vpOKonly, “Green Car Insurance Company Ltd” End Sub If you wanted your Details button to send an Email to a specific Email address through Microsoft Outlook type in the following code: Private Sub Commandbutton 3_click() Link ="mailto: GreenCarCompany@yahoo.co. uk” Msgbox “Click on OK to send an Email’, vboOKonly, ”Green Car Insurance Company Ltd” Activeworkbook.Followhyperlink Address: = link, newwindow:=True Unload me End Sub To link to a web site you would instead use the Hyperlink ink="heetp://www.Microsoft.com” for example. For addition VB programming, I would suggest you read the following book Excel 2007 VB programming with XML & ASP or check out the website hetp://www.excel-vba.com/excel-vba- contents.htm with its free and easy to use tutorials. Learn Microsoft Excel — Advanced Level 51 In order to return to our Menu page consisting of our buttons Exit, Enter and Details place a Command button on the Quotes worksheet and Edit it Return to Menu. The macro you would assign to this button is the Auto_open macro. The Filequote macro which you recorded earlier would also permit you to store customer information in an Access database. To do this open up Microsoft Access and choose a blank database. Rename all the field columns after the title headings in your Customer worksheet. Click on External Data and browse for the Excel spreadsheet you are currently working with and attach it. Check Show worksheets option and highlight Customer worksheet. Check the option button first row has column headers and click Next. Finally confirm linked table is Customer and click Finish. All Insurance customers will now be stored in an Access database, which could be sent as an Email attachment to other parties. That completes the quotation system interface. LearneMicrosoft Excel at Advanced-level is ideal for students studying a business / computerfelated-course or for the exe] Gale Mrslelne ialrsicc= ice) = It’s also effective for individuals im’business and commeree who may want to adopt-a more_efficient approach to howthey séng: data to one another, specifically by the use of data forms-and macros. ino =] Home Insert Page Layout Formulas Data Revie fie oe Po Record Macro ~ 2% (SF Properties » E! View Code Visual Macros Basic | Form Controls || Ae aca | iC") Ae & b| ActiveX Controls a] 928-1-899820-77- RRP: £9.99 noes. Aa el) This book is packed full of knowledge while at the same time being easy to understand. i M=r=laalialem progressive, trom linking workbooks effectively to creating your | own spréadsheet BS Cus nl clarcloce The book caters for | -any recent Excel version.and will | provide you with a lot.of fun-while-you learn some-sought= elim Olen sto ae Ee

You might also like