1. Trang chủ
  2. » Công Nghệ Thông Tin

Mastering Excel 2003 Programming with VBA phần 8 docx

61 276 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 61
Dung lượng 2,5 MB

Nội dung

4281book.fm Page 408 Sunday, February 29, 2004 5:12 PM 408 CHAPTER 17 XL(M) = XML Summary “XML, schmeXML. I don’t need no XML.” Before you dismiss XML as another over-hyped technology, you should give it due consideration. Because XML is becoming more and more prevalent, chances are that it will increasingly be an option when you are dealing with data transfer and data collaboration between disparate applications. Because XML is self-describing, it offers a much richer way to move data instead of text files. Addi- tionally, it is the preferred way to interact with web applications or services. In short, basic knowledge of XML is a useful tool for your development efforts. With the XML functionality added to Excel 2003, you now have the ability to read and write XML data using schemas other than XMLSS—Excel’s native spreadsheet schema. This is a monu- mental step forward in terms of incorporating Excel into business processes that are using XML. Prior to Excel’s new XML functionality, you had to write lots of code to use XML data in Excel in a useful manner. Whether you use Excel’s interface or you do it programmatically, there are three types of tasks to perform to integrate XML data with Excel. First, you create an XML map by associating a schema with a workbook. If you don’t have a schema (XSD file), you can show Excel an XML file and it will infer a schema based on that file. Second, you map elements in the schema to specific locations in the workbook. Finally, you can either import data from or export data to an XML file. When working with XML programmatically, it is important to understand how to identify spe- cific elements in an XML file. To do so, you use XPath. To map elements to a workbook, you set an object’s (either a Range object or a ListColumn object) XPath property using the SetValue method. Another object that you might want to invest a little time in becoming familiar with is the List- Object. ListObject is a new object in the Excel 2003 object model that is your programmatic gateway to manipulating worksheet lists. Excel uses such lists to display repeating XML elements. The conclusion of this chapter also wraps up the section of this book that covers external data source interaction. The next section deals with user interface design and development. I’ll kick this new section off with a chapter covering basic Excel user interfaces. 4281book.fm Page 409 Monday, March 1, 2004 1:19 AM Part 5 Enhancing the End User Experience In this section: ◆ Chapter 18: Basic User Interfaces ◆ Chapter 19: Taking Control of Command Bars ◆ Chapter 20: User Form Construction ◆ Chapter 21: One Smart Cookie: Smart Documents with Excel 2003 ◆ Chapter 22: Application Deployment Strategies This page intentionally left blank 4281book.fm Page 411 Monday, March 1, 2004 1:19 AM Chapter 18 Basic User Interfaces Nearly every application you develop except for the most elementary utilities requires some sort of user interface. Without a user interface, most general Excel users will be hard-pressed to use your application effectively. They may be able to view some of your procedures in the Macros dialog box, but how will they know which one to execute or when to execute it? One of the arguments I put forward early on in the book is that one of the benefits of using Excel as a development platform is that you can save a lot of time by building on top of its rich function- ality. One area where this is particularly true is when you’re developing a user interface. Excel offers quite a bit of functionality in terms of formatting, user interaction, and worksheet protection. Often you can creatively apply standard Excel functionality along with a few worksheet-based controls to get by. In this chapter, I’ll present the information you’ll need to build what I refer to as a worksheet- based user interface. A worksheet-based user interface is an Excel user interface that doesn’t use command bars (which are covered in Chapter 19) or user forms (covered in Chapter 20). User Interfaces in Excel For the purposes of this chapter, when I refer to the term user interface, I’m referring to the user interface specific to your Excel-based solution. A user interface enables end users to direct the flow of an appli- cation. Some of the tasks that a user interface must facilitate include these: ◆ Collecting user input ◆ Managing application settings and configuration ◆ Displaying output ◆ Providing application navigation facilities As the developer of an Excel application, you have many tools at your disposal regarding interface construction. Your application may include a number of different user interface elements. The following list displays some of the more common user interface elements found in Excel-based applications. Worksheet Form controls Worksheet Form controls are controls created using the Forms toolbar (View � Toolbars � Forms). Form controls were introduced with Excel 5.0 before 4281book.fm Page 412 Monday, March 1, 2004 1:19 AM 412 CHAPTER 18 BASIC USER INTERFACES ActiveX controls were available. Form controls are similar to ActiveX controls, but they differ in that they can be used on chart sheets and don’t have event procedures associated with them. Form controls are great as simple worksheet-based controls because you can use them without having to write any code. Worksheet ActiveX controls ActiveX controls are controls created using the Control Toolbox toolbar (View � Toolbars � Control Toolbox). ActiveX controls offer richer functionality when compared to their Form control counterparts, and they can be used on worksheets and user forms, as you will see in Chapter 20. To use ActiveX controls, you attach VBA code to various event pro- cedures associated with the control. You can modify the behavior and appearance of an ActiveX control by changing the properties associated with it. Built-in dialog boxes This category includes user interface elements found in the Excel object model or in VBA that allow you to interact with the user. Examples from this category include GetSaveAsFilename, GetOpenFilename, InputBox, and MsgBox. These kinds of elements can only be displayed using VBA. Custom user forms User forms are dialog boxes you design and build in the VBE. User forms offer the most flexibility. One negative aspect of user forms is that they can significantly increase the size of your workbook, so be sure to use them judiciously. User forms are covered in Chapter 20. Command bars Command bars include menus and toolbars. Using VBA, you can alter all of Excel’s existing menus and toolbars as well as display your own custom menu. Command bars are covered in Chapter 19. Native Excel functionality Last but not least, you can create a user interface that consists of the creative application of standard Excel worksheet functionality. Some of the features you might employ to do this include data validation, worksheet protection, cell locking, hyperlinks, shapes (items drawn using the Drawing toolbar), and cell formatting. So how do you choose from among the various elements when constructing an interface? Com- mand bars and custom user forms offer the most flexibility and look the most professional, but they require a lot of development effort. Worksheet-based components such as Form and ActiveX con- trols provide a fair amount of functionality with minimal effort. On the downside, they are specific to a worksheet and have limited visual appeal. ActiveX controls require some VBA programming; Form controls do not. All Excel applications use some native Excel functionality as part of the inter- face. I mean, it is an Excel application, isn’t it? In fact, you may get by with the creative application of native Excel functionality to provide a user interface. Paleozoic Controls 315 million seconds ago (1993 AD), Microsoft released Excel 5.0. This version of Excel quickly rose up to dominate the spreadsheet world due to a conglomeration of factors. One such factor was the inclusion of a way to embed user interface controls on a worksheet. These controls are referred to as form controls. Developers and power users alike soon embraced the controls as a way to enhance the user experience when they’re working with a complex Excel workbook. 4281book.fm Page 413 Monday, March 1, 2004 1:19 AM 413 PALEOZOIC CONTROLS Figure 18.1 The Forms toolbar Figure 18.2 Control properties determine what happens to a control when worksheet cells are moved or resized. Even today, form controls still occupy a niche in the Excel ecosystem. For one thing, they are nec- essary for backward compatibility. Additionally, form controls are the only kind of control that can be used on a chart sheet. Finally, for simple control needs, form controls are a little easier to work with than their ActiveX counterparts. Form controls live on a remote toolbar named Forms. You can visit them by selecting View � Toolbars � Forms. The Forms toolbar is shown in Figure 18.1. The Ubiquitous Button Buttons are probably the most common control. Even if you don’t need any of the other controls, you may elect to put a button somewhere in your workbook to allow users to launch a macro without hav- ing to select it from the Macros dialog box (Alt + F8). In fact, using a button to execute a macro is such a common action associated with buttons that when you add a button to a worksheet, Excel automatically displays a dialog box to assign a macro to the button. Tip For precise control placement, select the control and use the directional arrows. A few formatting options associated with buttons (and other controls for that matter) are worth inves- tigation—specifically, the items on the Properties tab of the Format control dialog box (Figure 18.2). The Format control dialog box can be displayed by right-clicking the control and selecting Format Control. The object positioning choices on this tab are described in the following list. To understand how these choices work, it helps to think in terms of positions. You’ll get a lot of exposure to this concept when you start constructing user forms in Chapter 20. For now, understand that an object’s position on the worksheet can be described by measuring the distance from the top of the worksheet to the 4281book.fm Page 414 Monday, March 1, 2004 1:19 AM 414 CHAPTER 18 BASIC USER INTERFACES top of the object and the distance from the left of the worksheet to the left edge of the object. One unit of distance is called a point. So the position (50,100) means that the object is 50 points from the top of the worksheet and 100 points from the left edge of the worksheet. To fully describe an object’s location on a worksheet, you also need to know the height and width of the object. Move and size with cells An object with this type of object positioning maintains a constant distance from the top left of the cell with which it is associated. It also maintains a constant dis- tance between the object’s lower-right corner and the top-left corner of the cell that contains the object’s lower-right corner. The cell that the object is associated with is the cell containing the object’s top-left corner. For example, if an object is associated with cell C5 and you insert a row above the fifth row thereby forcing cell C5 down to a new address of C6, the object will move down with the cell. The main behavior that is different with this option versus the other two options is that this option will also resize controls as you resize rows or columns associated with the cell. Move but don’t size with cells An object with this type of object positioning maintains a con- stant distance from the top left of the cell with which the object is associated. The object’s size remains constant regardless of how you resize the row or column. Don’t move or size with cells An object with this type of object positioning maintains a con- stant distance from the top left of the worksheet no matter what happens to the rows and columns. If an object’s position was originally 50,100 and you add 5 rows and resize all of the columns, the object will still be at 50,100. Likewise, the object’s width and height remain constant as you resize the row and column that the object is on top of. As you can see in Figure 18.2, you can also use this dialog box to specify whether the control should be output or not when printing. Figures 18.3 and 18.4 demonstrate the object positioning choices by showing a before and after shot of three buttons. Figure 18.3 These three buttons look alike 4281book.fm Page 415 Monday, March 1, 2004 1:19 AM 415 PALEOZOIC CONTROLS Figure 18.4 But they sure don’t act alike. Free to Choose For those occasions in which you want to allow your user to choose between a handful of options, you may want to present the choices using check boxes or option buttons. There is a distinct difference between check boxes and option buttons—the value or state of a check box is not dependent on any other check boxes, whereas option buttons operate in groups. An option button’s state is dependent on other option buttons in the same group. Only one option button in a particular group may be selected at a given time. Checkboxes are good candidates for situations in which you have a number of independent, two-state choices that you need to present to the user. These could be true/false, on/off, or yes/no choices. To link a check box control to a cell, right-click the check box and select Format Control. Then, set the cell link option on the Control tab (Figure 18.5) to the desired cell. You can change the text displayed by the check box by right-clicking the control and selecting Edit Text. Alternatively, hold down the Ctrl key while selecting the control and then select and edit the control’s text directly. When linking a control to a cell, occasionally it’s convenient to place the control directly on top of the cell to which you’re linking. If you change the fill of the control to automatic (on the Colors and Lines tab of the Format Control dialog box), the control will cover up the cell so that you can’t see the value underneath. For example, in Figure 18.6, notice that I have cell B5 selected. The value shown in the formula bar is true, but you can’t see the value on the worksheet because the control is covering it up. Check boxes can have a value state called Mixed. This state can’t be achieved by normal user inter- action with the control, it can only be achieved by checking the Mixed value in the Format dialog box or placing the error value #N/A in the cell to which the check box is linked. One potential use of check boxes is to allow users to easily show/hide worksheets in a workbook. In order to do this, you need to employ some VBA. Although I could go about this by creating a sep- arate procedure for each check box, because the process of showing/hiding worksheets happens so quickly, I opted for a single procedure (Listing 18.1) that sets the visibility of all of the worksheets every time a check box is clicked. The SetWorksheetVisibility procedure is the macro assigned to the View Worksheets check box group shown in Figure 18.6. As you click the various check boxes, the worksheet associated with the check box you click is displayed or hidden. 4281book.fm Page 416 Monday, March 1, 2004 1:19 AM 416 CHAPTER 18 BASIC USER INTERFACES Figure 18.5 The Format Control dialog box Figure 18.6 Check box controls are useful for allowing users to toggle settings on/off. Listing 18.1: Controlling Worksheet Visibility Sub SetWorksheetVisibility() Dim ws As Worksheet On Error Resume Next Set ws = ThisWorkbook.Worksheets("Checks and Options") Application.ScreenUpdating = False ThisWorkbook.Worksheets("Sheet1").Visible = _ CInt(ws.Range("ViewSheet1").Value) ThisWorkbook.Worksheets("Sheet2").Visible = _ CInt(ws.Range("ViewSheet2").Value) ThisWorkbook.Worksheets("Sheet3").Visible = _ CInt(ws.Range("ViewSheet3").Value) [...]... worksheet Figure 18. 17 shows the Properties win­ dow associated with a combo box Figure 18. 16 Testing a command button 427 4 28 CHAPTER 18 BASIC USER INTERFACES Figure 18. 17 The combo box has no shortage of properties Just browsing over the partial list of properties in Figure 18. 17 should give you an idea of how much more power the ActiveX combo box has compared to the Forms combo box Table 18. 3 lists some... couple VBA procedures In order to make things a little easier, I assigned range names to a few key cells Figure 18. 10 shows how the combo box is linked to the worksheet The ChildNumber range is cell D3 Cell C8 (named “Verdict”) is linked to the option buttons Finally, cell B14 is named “ChildList” Armed with this information, you shouldn’t have much trou­ ble comprehending Listing 18. 3 Figure 18. 9 Combo... trols is shown in Figure 18. 11 421 422 CHAPTER 18 BASIC USER INTERFACES Figure 18. 11 The scroll bar and spinner controls are useful for selecting a value relative to a range of values Figure 18. 12 shows the Control tab from a spinner control’s Format Control dialog box The Control tab for a scroll bar looks exactly the same except the Page Change option is enabled Figure 18. 12 Controlling the spin­... adjust column widths without allowing them to directly unprotect the worksheet to do it In order to do this, I used a spinner control in conjunction with some VBA code LIKE A KID IN A CANDY STORE Listing 18. 4 presents the code necessary to implement such a feature If you need to use this on a protected worksheet, you would need to add code to unlock and then relock the worksheet Listing 18. 4: Adjusting... richer programming interface than form controls, you can use any of the numerous ActiveX controls present on your com­ puter Figure 18. 14 shows a picture of the Control Toolbox toolbar In this figure, I have clicked the More Controls tool button and am browsing through the controls available on my computer Figure 18. 13 Using the spinner control 423 424 CHAPTER 18 BASIC USER INTERFACES Figure 18. 14 There... IN A CANDY STORE Figure 18. 15 The Properties win­ dow associated with a command button 4 Right-click the button and choose Properties A Properties window appears (Figure 18. 15) Take a minute to browse the properties associated with a command button I’ll cover some of the more common properties later in this section 5 Right-click the button and choose View Code You’ll see that Excel places you in the... and then enclosing them with a group box Figure 18. 7 shows an example of option controls used to indicate a report scaling preference In order to demonstrate how I put this together, I didn’t hide the control value behind the control as I did in the check box example Figure 18. 7 Option controls are used to allow users to select one of several potential options 417 4 18 CHAPTER 18 BASIC USER INTERFACES... As you can see, you can create combo boxes whose list is composed of multiple columns As an example, take a look at Figure 18. 18 The combo box shown has two columns of data and is linked to cells on a worksheet JUDGE A BOOK BY ITS COVER Figure 18. 18 You can have combo boxes with multiple columns of data It is not as obvious how to link an ActiveX combo box to cells on a worksheet You can’t just select... directly in the Properties window You can see this in the Properties window pictured in Figure 18. 17 This is the Properties window for the combo box shown in Figure 18. 18 Notice that the ListFillRange is Sheet2!B9:D13 Did you notice that the ColumnCount property is 3 yet only two columns show up in Figure 18. 18? To achieve this effect, I used the ColumnWidths property To control each column width, you... you need to know to have success with command bars Taking Inventory with CommandBars One way to get familiar with command bars is to take inventory of all the command bars in Excel You can achieve this by looping through all of the CommandBar objects in the CommandBars collection Listing 19.1 presents a procedure that loops through all of the CommandBar objects in Excel It loops through each of the . when they’re working with a complex Excel workbook. 4 281 book.fm Page 413 Monday, March 1, 2004 1:19 AM 413 PALEOZOIC CONTROLS Figure 18. 1 The Forms toolbar Figure 18. 2 Control properties. example. Figure 18. 7 Option controls are used to allow users to select one of several potential options. 4 281 book.fm Page 4 18 Monday, March 1, 2004 1:19 AM 4 18 CHAPTER 18 BASIC USER INTERFACES. in becoming familiar with is the List- Object. ListObject is a new object in the Excel 2003 object model that is your programmatic gateway to manipulating worksheet lists. Excel uses such lists

Ngày đăng: 13/08/2014, 15:20

TỪ KHÓA LIÊN QUAN