Excel 2002 Power Programming with VBA phần 10 docx

95 267 0
Excel 2002 Power Programming with VBA phần 10 docx

Đ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

852 Part VII ✦ Other Topics using a UserForm_QueryClose event procedure in the code module for the UserForm. The following example does not allow the user to close the form by click- ing the Close button: Private Sub UserForm_QueryClose _ (Cancel As Integer, CloseMode As Integer) If CloseMode = vbFormControlMenu Then MsgBox “You can’t close the form like that.” Cancel = True End If End Sub I’ve created a UserForm whose controls are linked to cells on the worksheet with the ControlSource property. Is this the best way to do this? In general, you should avoid using links to worksheet cells unless you absolutely must. Doing so can slow your application down because the worksheet is recalcu- lated every time a control changes the cell. Is there any way to create a control array for a UserForm? It’s possible with Visual Basic 6.0, but I can’t figure out how to do it with Excel VBA. You can’t create a control array, but you can create an array of Control objects. The following code creates an array consisting of all CommandButton controls: Private Sub UserForm_Initialize() Dim Buttons() As CommandButton Cnt = 0 For Each Ctl In UserForm1.Controls If TypeName(Ctl) = “CommandButton” Then Cnt = Cnt + 1 ReDim Preserve Buttons(1 To Cnt) Set Buttons(Cnt) = Ctl End If Next Ctl End Sub Is there any difference between hiding a UserForm and unloading a UserForm? Yes, the Hide method keeps the UserForm in memory but makes it invisible. The Unload statement unloads the UserForm, beginning the “termination” process (invoking the Terminate event for the UserForm) and removing the UserForm from memory. 4799-2 ch30.F 6/11/01 9:49 AM Page 852 853 Chapter 30 ✦ Frequently Asked Questions about Excel Programming How can I make my UserForm stay open while I do other things? By default, each UserForm is modal, which means that it must be dismissed before you can do anything else. Beginning with Excel 2000, however, you can make a UserForm modeless by writing vbModeless as the argument for the Show method. Here’s an example: UserForm1.Show vbModeless Excel 97 gives me a compile error when I write UserForm1.Show vbModeless. How can I make the form modeless in Excel 2002, while allowing it to remain modal in Excel 97? Test for the version of Excel that the user is running and then execute a separate procedure if the version is Excel 2000 or later. The following code demonstrates how: Sub ShowUserForm() If Val(Application.Version) >= 9 Then ShowModelessForm Else UserForm1.Show End If End Sub Sub ShowModelessForm() Dim frm As Object Set frm = UserForm1 frm.Show 0 ‘ vbModeless End Sub Because the ShowModelessForm procedure is not executed in Excel 97, it will not cause a compile error. I need to display a progress indicator like those you see when you’re installing software while a lengthy process is being executed. How can I do this? You can do this with a UserForm. Chapter 15 describes several different techniques, including one where I gradually stretch a shape inside a frame while the lengthy process is running. 4799-2 ch30.F 6/11/01 9:49 AM Page 853 854 Part VII ✦ Other Topics How can I use Excel’s drawing tools to create simple drawings on my UserForm? You can’t use the drawing tools directly with a UserForm, but you can do so indi- rectly. Start by creating the drawing on a worksheet. Then select the drawing and choose Edit ➪ Copy. Activate your UserForm and insert an Image object. Press F4 to display the Properties window. Select the Picture property and press Ctrl+V to paste the clipboard contents to the Image control. You may also need to set the AutoSize property to True. How can I generate a list of files and directories into my UserForm so the user can select a file from the list? There’s no need to do that. Use VBA’s GetOpenFilename method. This displays a “file open” dialog box in which the user can select a drive, directory, and file. I have several 1-2-3 for Windows files and Quattro Pro for Windows files that contain custom dialog boxes. Is there a utility to convert these to Excel dialog boxes? No. I need to concatenate strings and display them in a ListBox control. But when I do so, they aren’t aligned properly. How can I get them to display equal spacing between strings? You can use a monospaced font such as Courier New for the ListBox. A better approach, however, is to set up your ListBox to use two columns (see Chapter 14 for details). Is it possible to display a built-in Excel dialog box from VBA? Most, but not all, of Excel’s dialog boxes can be displayed by using the Application.Dialogs method. For example, the following instruction displays the dialog box that enables you to format numbers in cells: Application.Dialogs(xlDialogFormatNumber).Show Use the Object Browser to display a list of the constants for the built-in dialog boxes. Press F2 from the VBE, select the Excel library, and search for xlDialog. You’ll probably need to use some trial and error to locate the constant that corre- sponds to the dialog you want to display. 4799-2 ch30.F 6/11/01 9:49 AM Page 854 856 Part VII ✦ Other Topics Is it possible to create a UserForm box that lets the user select a range in a worksheet by pointing? Yes. Use the RefEdit control for this. See Chapter 14 for an example. Is there a way to change the start-up position of a UserForm? Yes, you can set the UserForm’s Left and Top properties. But for these to be effec- tive, you need to set the UserForm’s StartUpPosition property to 0. Can I add an Excel 5/95 dialog sheet to my workbook? Yes. Right-click any sheet tab in a workbook, and select Insert from the shortcut menu. In the Insert dialog box, select MS Excel 5.0 Dialog. Be aware that none of the information in this book applies to Excel 5/95 dialog sheets. Add-Ins Where can I get Excel add-ins? You can get Excel add-ins from a number of places: ✦ Excel includes several add-ins you can use whenever you need them. ✦ You can download more add-ins from Microsoft’s Office Update Web site. ✦ Third-party developers distribute and sell add-ins for special purposes. ✦ Many developers create free add-ins and distribute them via their Internet sites. ✦ You can create your own add-ins. How do I install an add-in? You can load an add-in by selecting either the Tools➪ Add-Ins command or the File ➪ Open command. Using Tools➪ Add-Ins is the preferred method. An add-in opened with File ➪ Open cannot be closed without using VBA. When I install my add-in using Excel’s Add-Ins dialog box, it shows up without a name or description. How can I give my add-in a description? Before creating the add-in, use the File ➪ Properties command to bring up the Properties dialog box. Click the Summary tab. In the Title box, enter the text that you want to appear in the Add-Ins dialog box. In the Comments field, enter the description for the add-in. Then create the add-in as usual. 4799-2 ch30.F 6/11/01 9:49 AM Page 856 857 Chapter 30 ✦ Frequently Asked Questions about Excel Programming I have several add-ins that I no longer use, yet I can’t figure out how to remove them from the Add-Ins Available list in the Add-Ins dialog box. What’s the story? Oddly, there is no way to remove unwanted add-ins from the list directly from Excel. You must edit the Windows Registry and remove the references to the add-in files you don’t want listed. Another way to do this is to move or delete the add-in files. Then when you attempt to open the add-in from the Add-Ins dialog box, Excel will ask if you want to remove the add-in from the list. How do I create an add-in? Activate any worksheet, and select File ➪ Save As. Then select Microsoft Excel Add- in (*.xla) from the Save as type drop-down list. I try to create an add-in, but the Save as type drop-down box doesn’t provide Add-in as an option. The most likely reason is that the active sheet is not a worksheet. Should I convert all my essential workbooks to add-ins? No! Although you can create an add-in from any workbook, not all workbooks are suitable. When a workbook is converted to an add-in, it is essentially invisible. For most workbooks, being invisible isn’t a good thing. Is it necessary to keep two copies of my workbook, the XLS version and the XLA version? With versions prior to Excel 97, maintaining an XLS and an XLA version was neces- sary. Beginning with Excel 97, however, this is no longer necessary. An add-in can be converted back to a normal workbook. How do I modify an add-in after it’s been created? Activate the VBE (Alt+F11), and set the IsAddIn property of the ThisWorkbook object to False. Make your changes, set the IsAddIn property to True, and resave the file. What’s the difference between an XLS file and an XLA file created from an XLS file? Is the XLA version compiled? Does it run faster? There isn’t a great deal of difference between the files, and you generally won’t notice any speed differences. VBA code is always “compiled” before it is executed. This is true whether it’s in an XLS file or an XLA file. However, XLA files contain the actual VBA code, not compiled code. 4799-2 ch30.F 6/11/01 9:49 AM Page 857 858 Part VII ✦ Other Topics How do I protect the code in my add-in from being viewed by others? Activate the VBE and select Tools➪ xxxx Properties (where xxxx is the name of your project). Click the Protection tab, select Lock project for viewing, and enter a password. Are my XLA add-ins safe? In other words, if I distribute an XLA file, can I be assured that no one else will be able to view my code? Protect your add-in by locking it with a password. This prevents most users from being able to access your code. Recent versions of Excel have improved the secu- rity features, but the password still may be broken by using any of a number of utili- ties. Bottom line? Don’t think of an XLA as being a secure file. CommandBars I have a macro attached to a toolbar button. Is it possible to have the macro perform a different action if the user presses Shift while the button is clicked? Yes, but you have to use a Windows API call to do it. Refer to Chapter 11 for details. Excel 95 had a handy menu editor, but it’s missing in Excel 97 and later versions. What gives? Beginning with Excel 97, the toolbars and menus in Excel are entirely different. Both are called CommandBars. The menu editor is gone, but users can edit CommandBars by using the Customize dialog box (select Tools➪ Customize). Can I edit menus created by Excel 95’s menu editor? Yes, but you’ll need to do so in Excel 95. When I change a menu with the Customize dialog box, the menu is changed permanently. How can I make the menu change apply to only one workbook? You’ll need to perform your menu changes with VBA code when the workbook is opened, and restore the menu to normal when the workbook is closed. I know you can use the FaceId property to add an image to a toolbar control. But how do I figure out which FaceId value goes with a particular image? Microsoft didn’t provide any way to do this, but several utilities exist that make it easy to identify the FaceId values. See Chapter 22 for an add-in that you might find helpful. 4799-2 ch30.F 6/11/01 9:49 AM Page 858 859 Chapter 30 ✦ Frequently Asked Questions about Excel Programming I attached a new version of my toolbar to a workbook, but Excel continues to use the older version. How do I get it to use the new version of my toolbar? When Excel opens a workbook that has an attached toolbar, it displays the toolbar only if one with the same name does not already exist on the user’s system. The best solution is to write VBA code to create the toolbar on the fly when the work- book is opened and to delete it when the workbook is closed. Alternatively, you can attach the toolbar to the workbook and then write code to delete the toolbar when the workbook is closed. I’ve made lots of changes to Excel’s toolbars. How can I restore all of these toolbars to their original state? You can use the Customize dialog box and reset each one manually. Or run the fol- lowing procedure: Sub ResetAllToolbars() For Each tb In CommandBars If tb.Type = msoBarTypeNormal Then If tb.BuiltIn Then tb.Reset End If Next tb End Sub Be aware that this procedure will also remove any toolbar customizations per- formed by add-ins. How can I set things up so my custom menu is displayed only when a particular workbook is active? You need to make use of the WorkbookActivate and WorkbookDeactivate events. In other words, write procedures in the code module for the ThisWorkbook object that hide the custom menu when the workbook is deactivated and unhide the cus- tom menu when the workbook is activated. How can I add a “spacer” between two buttons on a toolbar? Set the BeginGroup property of the control after the spacer to True. How do you display a check mark next to a menu item? A check mark on a menu item is controlled by the menu item’s State property. The following instruction, for example, displays a check mark next to the menu item called My Item: CommandBars(1).Commands(“MyMenu”). _ Commands(“My Item”).State = msoButtonDown To uncheck the menu item, set the State property to msoButtonUp. 4799-2 ch30.F 6/11/01 9:49 AM Page 859 860 Part VII ✦ Other Topics I accidentally deleted some items from the Worksheet menu and can’t get them back. Restarting Excel doesn’t fix it. Select Tools➪ Customize, and select the Toolbars tab in the Customize dialog box. Select the Worksheet Menu Bar item, and click the Reset button. How can I disable all the right-click shortcut menus? The following procedure will do the job: Sub DisableAllShortcutMenus() Dim cb As CommandBar For Each cb In CommandBars If cb.Type = msoBarTypePopup Then _ cb.Enabled = False Next cb End Sub Is there a way to disable the shortcut menus that appear when the user clicks the right mouse button? Yes, the following instruction will do the job: CommandBars(“Toolbar List”).Enabled = False I just entered CommandBars(“Toolbar List”).Enabled = False, and it doesn’t work on my system! The original version of Excel 97 had a problem with this instruction. It was cor- rected in the SR-1 service release for Excel 97. ✦✦✦ 4799-2 ch30.F 6/11/01 9:49 AM Page 860 Excel Resources Online I f I’ve done my job, the information provided in this book will be useful to you. It is, however, by no means compre- hensive. In addition, new issues tend to crop up, so you’ll want to make sure that you’re up to date. Therefore, I’ve com- piled a list of additional resources that may help you become more proficient in Excel application development. I’ve classi- fied these resources into three categories: ✦ Microsoft technical support ✦ Internet newsgroups ✦ Internet Web sites Microsoft Technical Support Technical support is the common term for assistance pro- vided by a software vendor. In this case, I’m talking about assistance that comes directly from Microsoft. Microsoft’s technical support is available in several different forms. Support options To find out your support options, choose the Help ➪ About Microsoft Excel command. Then click the Tech Support but- ton. This opens a help file that lists all the support options offered by Microsoft, including both free and fee-based support. My experience is that you should use vendor standard tele- phone support only as a last resort. Chances are, you’ll run up a big phone bill (assuming that you can even get through) and spend lots of time on hold, but you may or may not find an answer to your question. A A APPENDIX ✦✦✦✦ 4799-2 AppA.F 6/11/01 9:49 AM Page 861 [...]... &H5C 0101 1100 \ 93 ] &H5D 0101 1101 ] 94 ^ &H5E 0101 1 110 ^ 95 _ &H5F 0101 1111 _ 96 ` &H60 0 110 0000 ` 97 a &H61 0 110 0001 a 98 b &H62 0 110 0 010 b 99 c &H63 0 110 0011 c 100 d &H64 0 110 0100 d 101 e &H65 0 110 0101 e 102 f &H66 0 110 0 110 f 103 g &H67 0 110 0111 g 104 h &H68 0 110 1000 h 105 i &H69 0 110 1001 i 106 j &H6A 0 110 1 010 j 107 k &H6B 0 110 1011 k 108 l &H6C 0 110 1100 l 109 m &H6D 0 110 1101 m 110. .. &HA2 101 0 0 010 Alt+0162 163 Ê &HA3 101 0 0011 Alt+0163 &HA4 101 0 0100 Alt+0164 164 165 Ơ &HA5 101 0 0101 Alt+0165 166 _ &HA6 101 0 0 110 Alt+0166 167 Đ &HA7 101 0 0111 Alt+0167 168 ă &HA8 101 0 100 0 Alt+0168 169 (c) &HA9 101 0 100 1 Alt+0169 170 ê &HAA 101 0 101 0 Alt+0170 171 ô &HAB 101 0 101 1 Alt+0171 172 ơ &HAC 101 0 1100 Alt+0172 173 _ &HAD 101 0 1101 Alt+0173 174 (r) &HAE 101 0 1 110 Alt+0174 175 &HAF 101 0 1111... 0100 100 1 I 74 J &H4A 0100 101 0 J 75 K &H4B 0100 101 1 K 76 L &H4C 0100 1100 L 77 M &H4D 0100 1101 M 78 N &H4E 0100 1 110 N 79 O &H4F 0100 1111 O 80 P &H50 0101 0000 P 81 Q &H51 0101 0001 Q 82 R &H52 0101 0 010 R 83 S &H53 0101 0011 S 84 T &H54 0101 0100 T 85 U &H55 0101 0101 U 86 V &H56 0101 0 110 V 87 W &H57 0101 0111 W 88 X &H58 0101 100 0 X 89 Y &H59 0101 100 1 Y 90 Z &H5A 0101 101 0 Z 91 [ &H5B 0101 101 1... 138 _ &H8A 100 0 101 0 Alt+0138 139 &H8B 100 0 101 1 Alt+0139 140 &H8C 100 0 1100 Alt+0140 141 _ &H8D 100 0 1101 Alt+0141 142 _ &H8E 100 0 1 110 Alt+0142 143 _ &H8F 100 0 1111 Alt+0143 144 _ &H90 100 1 0000 Alt+0144 145 &H91 100 1 0001 Alt+0145 146 &H92 100 1 0 010 Alt+0146 147 &H93 100 1 0011 Alt+0147 148 &H94 100 1 0100 Alt+0148 149 &H95 100 1 0101 Alt+0149 150 &H96 100 1 0 110 Alt+0150 151 &H97 100 1 0111... 0 010 1001 ) 42 * &H2A 0 010 1 010 * 43 + &H2B 0 010 1011 + 44 , &H2C 0 010 1100 , 45 - &H2D 0 010 1101 - 46 &H2E 0 010 1 110 47 / &H2F 0 010 1111 / 48 0 &H30 0011 0000 0 49 1 &H31 0011 0001 1 50 2 &H32 0011 0 010 2 51 3 &H33 0011 0011 3 52 4 &H34 0011 0100 4 53 5 &H35 0011 0101 5 54 6 &H36 0011 0 110 6 55 7 &H37 0011 0111 7 56 8 &H38 0011 100 0 8 57 9 &H39 0011 100 1 9 58 : &H3A 0011 101 0 : 59 ; &H3B 0011 101 1... 100 1 100 0 Alt+0152 4799-2 AppD.F 6/11/01 9:49 AM Page 887 Appendix D ANSI Code Reference ANSI code Character Hex code Binary code Keystroke* 153 (tm) &H99 100 1 100 1 Alt+0153 154 _ &H9A 100 1 101 0 Alt+0154 155 &H9B 100 1 101 1 Alt+0155 156 &H9C 100 1 1100 Alt+0156 157 _ &H9D 100 1 1101 Alt+0157 158 _ &H9E 100 1 1 110 Alt+0158 159 &H9F 100 1 1111 Alt+0159 160 &HA0 101 0 0000 Alt+0160 161 Ă &HA1 101 0... { &H7B 0111 101 1 { 124 | &H7C 0111 1100 | 125 } &H7D 0111 1101 } 126 ~ &H7E 0111 1 110 ~ 127 _ &H7F 0111 1111 Del &H80 100 0 0000 Alt+0128 128 129 _ &H81 100 0 0001 Alt+0129 130 &H82 100 0 0 010 Alt+0130 131 &H83 100 0 0011 Alt+0131 132 &H84 100 0 0100 Alt+0132 133 &H85 100 0 0101 Alt+0133 134 &H86 100 0 0 110 Alt+0134 135 &H87 100 0 0111 Alt+0135 136 &H88 100 0 100 0 Alt+0136 137 &H89 100 0 100 1 Alt+0137... &H3C 0011 1100 < 61 = &H3D 0011 1101 = Continued 883 4799-2 AppD.F 884 6/11/01 9:49 AM Page 884 Appendixes ANSI code Character Hex code Binary code Keystroke* 62 > &H3E 0011 1 110 > 63 ? &H3F 0011 1111 ? 64 @ &H40 0100 0000 @ 65 A &H41 0100 0001 A 66 B &H42 0100 0 010 B 67 C &H43 0100 0011 C 68 D &H44 0100 0100 D 69 E &H45 0100 0101 E 70 F &H46 0100 0 110 F 71 G &H47 0100 0111 G 72 H &H48 0100 100 0 H 73... 0001 1101 30 &H1E 0001 1 110 4799-2 AppD.F 6/11/01 9:49 AM Page 883 Appendix D ANSI Code Reference ANSI code Character Hex code Binary code Keystroke* 31 &H1F 0001 1111 32 &H20 0 010 0000 Space 33 ! &H21 0 010 0001 ! 34 &H22 0 010 0 010 35 # &H23 0 010 0011 # 36 $ &H24 0 010 0100 $ 37 % &H25 0 010 0101 % 38 & &H26 0 010 0 110 & 39 &H27 0 010 0111 40 ( &H28 0 010 1000... 0 010 3 &H03 0000 0011 4 &H04 0000 0100 5 &H05 0000 0101 6 &H06 0000 0 110 7 &H07 0000 0111 8 &H08 0000 100 0 Backspace 9 &H09 0000 100 1 Tab 10 &H0A 0000 101 0 11 &H0B 0000 101 1 12 &H0C 0000 1100 13 &H0D 0000 1101 Return 14 &H0E 0000 1110 . A-1 Microsoft.com’s Excel- Related Newsgroups Newsgroup Topic microsoft.public. Programming Excel with VBA or XLM macros excel. programming microsoft.public. Converting 1-2-3 or Quattro Pro sheets into Excel. Building charts with Excel excel.charting microsoft.public. Printing with Excel excel.printing microsoft.public. Using Microsoft Query and Data Access Objects (DAO) excel. queryDAO in Excel microsoft.public descriptive. Postings with a subject line such as “Help me!” and Excel Question” are less likely to be answered than postings with a subject such as VBA Code to Resize a Chart in Excel 2002. ” 3. Specify

Ngày đăng: 14/08/2014, 02:20

Tài liệu cùng người dùng

Tài liệu liên quan