Excel & Visual Basic for Applications (VBA) user interfaces o on-sheet buttons o toolbar buttons and custom toolbars o custom menus o InputBox and MsgBox functions o userforms 1 On-Sheet Buttons 1) use the Forms toolbar to place a button on the worksheet 2) assign a macro to the button 3) format the button as desired activate the Forms toolbar from the View menu 2 1
On-Sheet Buttons draw button on worksheet assign button to macro [ Note: assignment asked for automatically in Excel 2002] 3 On-Sheet Buttons change button text change font style 4 2
On-Sheet Buttons click button to run macro use on-sheet buttons for macros dedicated to the workbook and worksheet, often to launch userforms 5 Toolbar Buttons use toolbar buttons for tasks common to many workbooks, like editing tasks, e.g., center and wrap a title in the cell Tools Customize Commands tab Macros 6 3
Toolbar Buttons click on "happy face" button change button image drag and place on formatting toolbar in a convenient location pick from collection of standard buttons, or... 7 Toolbar Buttons Button Editor select Edit Button Image... erase the happy face (yea!) create new design 8 4
Toolbar Buttons select Assign Macro... macro typically from Personal.xls, in order to be general to all open workbooks it is possible to add toolbar buttons when a certain workbook opens and remove them when the workbook closes, but this takes some extra work and is unusual 9 Toolbar Buttons change button name to indicate function close Customize window test button and macro select headings & click CW button result widen column manually 10 5
Toolbar Buttons toolbar buttons are mainly of use on your own computer system, not one of the lab systems they are best used for special formatting macros that you want to apply to a variety of worksheets in different workbooks toolbar buttons are sometimes used to run macros that are part of an Add-In these are usually put on their own custom toolbar which is installed and "docked" with the other toolbars when the Add-In is activated 11 Custom Toolbars Tools Customize Toolbars tab click New... button type in toolbar name: new, empty toolbar created and docked at the top 12 6
Custom Toolbars drag CW button to new toolbar more buttons could be added a single toolbar is used to organize buttons that have a common theme, e.g., special formatting buttons or a dedicated engineering Add-In like calculations for flow in pipelines once again, it is possible to display an entire toolbar only when a given workbook is opened and have it removed when the workbook is closed 13 Custom Menus Tools Customize Commands tab New Menu category click on New Menu and drag to menu bar 14 7
Custom Menus change menu name drag commands to menu, both custom & built-in begin a menu group drag to menu completed menu Modify Selection 15 InputBox Function simplified version InputBox( prompt, title, default value ) optional typical use Xin = InputBox( Enter value for x: ) [ assign result of InputBox to variable for later use ] complete version InputBox( prompt, title,default,xpos,ypos,helpfile,context) optional 16 8
InputBox Method (alternative to InputBox Function) object.inputbox( prompt, title, default, xpos, ypos, helpfile, context, type) type codes 0 formula 1 number 2 string 4 logical 8 cell reference 16 error value, e.g., #N/A 64 array of values optional codes can be combined to allow for inputs of multiple types, e.g., 3 for number or string 17 InputBox Method (alternative to InputBox Function) 18 9
MsgBox Function MsgBox( prompt,buttons, title,helpfile,context) optional text displayed in the message box prompt items must be combined with & MsgBox The answer is & Xval & tons button codes vbokonly 0 vbexclamation 48 vbokcancel 1 vbinformation 64 vbabortretryignore 2 vbdefaultbutton1 0 vbyesnocancel 3 vbdefaultbutton2 256 vbyesno 4 vbdefaultbutton3 512 vbretrycancel 5 vbdefaultbutton4 768 vbcritical 16 vbsystemmodal 4096 vbquestion 32 MsgBox result is button clicked by user 19 MsgBox Function MsgBox Return Value vbok 1 vbcancel 2 vbabort 3 vbretry 4 vbignore 5 vbyes 6 vbno 7 Putting line breaks into the message displayed 20 10
UserForms (custom dialog boxes) the most flexible (and complicated) user interface feature Illustrate by example: a typical engineering formula Calculate the Reynolds Number for liquid flow in a pipe Du Re= ρ ρ : fluid density D: pipe inside diameter µ u: average fluid velocity µ : viscosity Typical units and values kg lbm : 900 55 m ft D: 55 mm 2.09 in ρ 3 u: 3 m ft 2 5 µ : 0.005 Pa s s s 3cP 21 UserForms Insert a UserForm in the Visual Basic Editor (VBE) New and empty UserForm 22 11
UserForms change name and title Properties window lists properties of selected object, here the entire userform 23 UserForms Add title and field for density Add textbox for value change caption Properties window now refers to the label object 24 12
UserForms Add a frame to contain the units choices remove frame caption Add an option button in the frame for one units choice change title & caption stretch button so caption shows 25 UserForms change default button value to true add button for alternate units with caption and default false add similar entries for pipe diameter, velocity and viscosity 26 13
UserForms add a command button to compute the answer change command button name, caption & font 27 UserForms add a Quit button with similar formatting the UserForm is complete, but two important background elements are missing: 1) there is no way for the UserForm to appear for use 2) when the Compute and Quit buttons are clicked, nothing will happen there is no VBA code behind them We ll deal with 2) first. 28 14
UserForms double-click the Compute Re button here, we must add code to carry out the calculations necessary and display the Reynolds Number result convert units, as required compute the Reynolds Number and display the result 29 UserForms insert a new module and add this Sub to start the UserForm double-click the Quit button and add the code to remove ( unload ) the UserForm Run the StartReynoldsNumber macro and enter values with units selected 30 15
UserForms click the Compute Re button click OK then or 1) you can set up and compute other cases 2) you click the Quit button to close out the UserForm UserForms can be loaded automatically when the workbook is opened. This is called an event handler. And they can be unloaded automatically when the workbook is closed. Although this is a good example of the use of UserForms, there are many other options and possibilities. You may need a good VBA reference book to take advantage fully of the potential of UserForms. 31 16