Presentation is loading. Please wait.

Presentation is loading. Please wait.

XP Week 6 – ABC By Aurino Djamaris Bakrie School of Management.

Similar presentations


Presentation on theme: "XP Week 6 – ABC By Aurino Djamaris Bakrie School of Management."— Presentation transcript:

1 XP Week 6 – ABC By Aurino Djamaris Bakrie School of Management

2 XP Error handling, Debugging and User Interface within Excel Error handling Techniques Debugging creating dialog boxes, userform basics, using userform controls, and implement some techniques and tricks, using macro through user interface

3 XP Error-Handling Techniques Identifying errors Doing something about the errors that occur Recovering from errors Creating intentional errors (Yes, sometimes an error can be a good thing.) write code that avoids displaying Excel’s error messages as much as possible

4 XP Excel Error Messages Excel responds with the message shown below, indicating that your code generated a run-time error Excel displays this error message when the Procedure attempts to Calculate the square root of a negative number. Excel displays this error message when the Procedure attempts to Calculate the square root of a negative number.

5 XP Error Handling Anticipate this error and handle it. Example : Sub EnterSquareRoot () Error Handling 1 Error Handling 2

6 XP Handling Errors Another Way adding an On Error statement to trap all errors and then checking to see whether the InputBox was cancelled.

7 XP On Error Problem? Using an On Error statement in your VBA code causes Excel to bypass its built-in error handling and use your own error- handling code.

8 XP On Error Statements

9 XP Resuming on Error

10 XP Example: Resume after an error occurs

11 XP What Error and Err Number Trap Error Treat Error accordingly

12 XP An Intentional Error The Macro1 procedure (which must be in the same project as WorkbookOpen) calls the WorkbookOpen function and passes the workbook name (Prices.xlsx) as an argument.

13 XP Bug Extermination Techniques Defining a bug and why you should squash it Recognizing types of program bugs you may encounter Using techniques for debugging your code Using the VBA built-in debugging tools

14 XP The bugs categories Logic flaws in your code. Incorrect context bugs Extreme-case bugs Wrong data type bugs Wrong version bugs Beyond-your-control bugs

15 XP Identifying Bugs An error message like this often means that your VBA code contains a bug. The best debugging approach is thorough testing, under a variety of real-life conditions

16 XP Debugging Techniques Four most common methods for debugging Excel VBA code: Examining the code: taking a close look at your code Inserting MsgBox functions at various locations in your code: – MsgBox LoopIndex & “ “ & CellCount – MsgBox LoopIndex & vbNewLine & CellCount & vbNewLine & MyVal – MsgBox ActiveSheet.Name & “ “ & TypeName(ActiveSheet) Inserting Debug.Print statements – Debug.Print LoopIndex, CellCount, MyVal Using the Excel built-in debugging tools: – Excel includes a set of debugging tools that can help you correct problems in your VBA code.

17 XP the Debugger set a breakpoint in your VBA code

18 XP Using the Immediate window display the VBE’s Immediate window  pressing Ctrl+G. for finding the current value of any variable in your program.

19 XP Bug Reduction Tips Use an Option Explicit statement at the beginning of your modules. Format your code with indentation. Be careful with the On Error Resume Next statement. Use lots of comments. Nothing is more frustrating than revisiting code Keep your Sub and Function procedures simple. Use the macro recorder to help identify properties and methods. Understand Excel’s debugger.

20 XP VBA Programming Examples Working with ranges Changing Excel settings Working with charts Speeding up your VBA code

21 XP Working with ranges Copying a range more efficiently

22 XP Copying a variable-sized range or

23 XP Selecting to the end of a row or column use the CurrentRegion property to select an entire block of cells. End method takes one argument  constants use: – xlUp – xlDown – xlToLeft – xlToRight

24 XP Selecting to the end of a row or column (cont) Selecting a row or column Moving a range Looping through a range efficiently

25 XP Skip Blanks examples processing only the nonblank cells using the SpecialCells method. the selection’s subset that consists of cells with constants and the selection’s subset that consists of cells with formulas

26 XP Prompting for a cell value use VBA’s InputBox function to get a value from the user. or

27 XP Other selection methods Identifying a multiple selection Determining the selection type determine whether the user made a multiple selection determine whether the user made a multiple selection displays a message and exits the procedure if the current selection is not a Range object: displays a message and exits the procedure if the current selection is not a Range object:

28 XP Changing Excel Settings Changing Boolean settings Changing non-Boolean settings toggles the calculation mode between manual and automatic the Not operator to effectively toggle the page break display

29 XP Working with Charts Modifying the chart type Looping through the ChartObjects collection

30 XP Working with Charts (cont) Modifying chart properties

31 XP Working with Charts (cont) Applying chart formatting

32 XP VBA Speed Tips Turning off screen updating – Application.ScreenUpdating = False / True Turning off automatic calculation – Application.Calculation = xlCalculationManual / xlCalculationAutomatic Eliminating those pesky alert messages – Application.DisplayAlerts = False /True Simplifying object references – defining this object variable, then define the value rather than defining this object variable and the value in single line Declaring variable types – use the data type that requires the smallest number of bytes yet can still handle all the data assigned to it.

33 XP Using the With-End With structure Use Rather than

34 XP USER INTERFACES Simple Dialog Boxes UserForm Basics UserForm Controls UserForm Techniques and Tricks Accessing Your Macros Through the User Interface

35 XP Simple Dialog Boxes The MsgBox function The InputBox function The GetOpenFileName method The GetSaveAsFileName method

36 XP MsgBox MsgBox(prompt[, buttons][, title])

37 XP InputBox InputBox(prompt[, title][, default])

38 XP GetOpenFileName object.GetOpenFilen ame([fileFilter], [filterIndex], [title], [buttonText], [multiSelect])

39 XP Selecting Multiple File

40 XP GetSaveAsFileName object.GetSaveAsFilename([initialFilename], [fileFilter],[filterIndex], [title], [buttonText]) The Excel GetSaveAsFilename method works just like the GetOpenFilename method, but it displays the Excel Save As dialog box rather than its Open dialog box. The GetSaveAsFilename method gets a path and filename from the user but doesn’t do anything with it.

41 XP Getting a folder name allows the user to select a directory.

42 XP UserForm Basics Creating UserForms 1.Determine how the dialog box will be used 2.Press Alt+F11 to activate the VBE and insert a new UserForm object. 3.Add controls to the UserForm. 4.Use the Properties window to modify the properties for the controls or for the UserForm itself. 5.Write event-handler procedures for the controls (for example, a macro that executes when the user clicks a button in the dialog box). 6.Write a procedure (stored in a VBA module) that displays the dialog box to the user.

43 XP Inserting a new UserForm 1. Activate the VBE by pressing Alt+F11. 2. Select the workbook in the Project window. 3. Choose Insert ➪ UserForm.

44 XP Adding controls to a UserForm You use the tools in the Toolbox to add controls to UserForm.

45 XP Changing properties for a UserForm control Properties for controls include the following: 1. Name 2. Width 3. Height 4. Value 5. Caption

46 XP Viewing the UserForm Code window Every UserForm object has a Code module that holds the VBA code (the event-handler procedures) executed when the user works with the dialog box. To view the Code module, press F7. The Code window is empty until you add some procedures. Press Shift+F7 to return to the dialog box.

47 XP Displaying a UserForm The macro that displays the dialog box must be in a VBA module — not in the Code window for the UserForm.

48 XP Using information from a UserForm The VBE provides a name for each control you add to a UserForm. The control’s name corresponds to its Name property. Use this name to refer to a particular control in your code.

49 XP A UserForm Example Creating the UserForm 1.Press Alt+F11 to activate the VBE. 2.If multiple projects are in the Project window, select the project that corresponds to the workbook you’re using. 3.Choose Insert ➪ UserForm. 4.Press F4 to display the Properties window. 5.In the Properties window, change the dialog box’s Caption property to Change Case. 6.The dialog box is a bit too large, so you may want to click it and use the handles to make it smaller.

50 XP Adding the CommandButtons 1.Make sure that the toolbox is displayed. If it isn’t, choose View ➪ Toolbox. 2.If the Properties window isn’t visible, press F4 to display it. 3.In the toolbox, drag a CommandButton into the dialog box to create a button. 4.Make sure that the CommandButton is selected. Then activate the Properties window and change the following properties: 5.Add a second CommandButton object to the UserForm and change thefollowing properties: 6.Adjust the size and position of the controls so your dialog box looks something like Figure 16-5.

51 XP Figure 16-5

52 XP Adding the OptionButtons 1.In the toolbox, click the Frame tool and drag in the dialog box. 2.Use the Properties window to change the frame’s caption to Options. 3.In the Toolbox, click the OptionButton tool and drag in the dialog box (within the Frame). 4.Select the OptionButton and use the Properties window to change the following properties: 5.Add another OptionButton and use the Properties window to change the following properties:

53 XP Adding the OptionButtons 6.Add a third OptionButton and use the Properties window to change the following properties: 7.Adjust the size and position of the OptionButtons, Frame, and dialog box.

54 XP Adding event-handler procedures 1.Double-click the Cancel button. 2.Insert the following statement inside the procedure (before the End Sub statement): 3.Press Shift+F7 to return to the UserForm. 4.Double-click the OK button. 5.Enter the following code inside the procedure:

55 XP

56 Creating a macro to display the dialog box 1.In the VBE window, choose Insert ➪ Module. 2.Enter the following code:

57 XP Making the macro available 1.Activate the Excel window via Alt+F11. 2.Choose Developer ➪ Code ➪ Macros or press Alt+F8. 3.In the Macros dialog box, select the ChangeCase macro. 4.Click the Options button. 5.Enter an uppercase C for the Shortcut key. 6.Enter a description of the macro in the Description field. 7.Click OK. 8.Click Cancel when you return to the Macro dialog box.

58 XP Testing the macro 1. Activate a worksheet (any worksheet in any workbook). 2. Select some cells that contain text. 3. Press Ctrl+Shift+C. 4. Make your choice and click OK.

59 XP UserForm Controls Adding controls 1.Click the Toolbox tool that corresponds to the control you want to add. 2.Click in the UserForm. 3.Drag the control into position.

60 XP Introducing control properties

61 XP

62 Change properties by selecting from a dropdown list of valid property values.

63 XP Dialog Box Controls: The Details CheckBox control Accelerator: A letter that lets the user change the value of the control by using the keyboard. For example, if the accelerator is A, pressing Alt+A changes the value of the CheckBox control (from checked to unchecked, or from unchecked to checked). ControlSource: The address of a worksheet cell that’s linked to the CheckBox. The cell displays TRUE if the control is checked or FALSE if the control is not checked. Value: If True, the CheckBox has a check mark. If False, it does not have a check mark.

64 XP ComboBox control is a drop-down box and displays only one item at a time

65 XP CommandButton control

66 XP Other controls Frame control Image control Label control ListBox control MultiPage control OptionButton control RefEdit control ScrollBar control SpinButton control TabStrip control TextBox control ToggleButton control

67 XP UserForm Techniques and Tricks Using Dialog Boxes A UserForm Example – Creating the dialog box – Writing code to display the dialog box – Making the macro available – Trying out your dialog box – Adding event-handler procedures – Validating the data – Now the dialog box works More UserForm Examples – A ListBox example – Selecting a range – Using multiple sets of OptionButtons – Using a SpinButton and a TextBox – Using a UserForm as a progress indicator – Creating a tabbed dialog box – Displaying a chart in a dialog box A Dialog Box Checklist See Chapter 18. UserForm Techniques and Tricks

68 XP Accessing Macros Through the User Interface

69 XP Working with CommandBars

70 XP VBA Shortcut Menu Examples

71 XP Add to shortcut menu

72 XP Create a toolbar


Download ppt "XP Week 6 – ABC By Aurino Djamaris Bakrie School of Management."

Similar presentations


Ads by Google