Excel 2019 Power Programming with VBA. Michael Alexander

Чтение книги онлайн.

Читать онлайн книгу Excel 2019 Power Programming with VBA - Michael Alexander страница 19

Excel 2019 Power Programming with VBA - Michael Alexander

Скачать книгу

shortcut menus

      

Chapter 18, “Working with Shortcut Menus,” describes how to work with shortcut menus using VBA, including some limitations due to the single document interface introduced in Excel 2013.

      Creating shortcut keys

      Another user interface option at your disposal is a custom shortcut key. Excel lets you assign a Ctrl key (or Shift+Ctrl key) combination to a macro. When the user presses the key combination, the macro executes.

      There are two caveats, however. First, make it clear to the user which keys are active and what they do. Second, do not assign a key combination that's already used for something else. A key combination that you assign to a macro takes precedence over the built-in shortcut keys. For example, Ctrl+S is a built-in Excel shortcut key used to save the current file. If you assign this key combination to a macro, you lose the capability to save the file with Ctrl+S. Remember that shortcut keys are case sensitive, so you can use a combination such as Ctrl+Shift+S.

      Creating custom dialog boxes

      A custom dialog box is known as a UserForm. A UserForm can solicit user input, get a user's options or preferences, and direct the flow of your entire application. The elements that make up a UserForm (buttons, drop-down lists, check boxes, and so on) are called controls—more specifically, ActiveX controls. Excel provides a standard assortment of ActiveX controls, and you can also incorporate third-party controls.

      After adding a control to a dialog box, you can link it to a worksheet cell so that it doesn't require any macros (except a simple macro to display the dialog box). Linking a control to a cell is easy, but it's not always the best way to get user input from a dialog box. Most of the time, you want to develop VBA macros that work with your custom dialog boxes.

      

We cover UserForms in detail in Part III.

      Using ActiveX controls on a worksheet

Screenshot of a simple worksheet model with several UserForm controls inserted directly in the worksheet, and the sheet contains the following ActiveX controls: a CheckBox, a ScrollBar, and two sets of OptionButtons.

      ON THE WEB

      This workbook is available on this book's website. The file is named worksheet controls.xlsx.

      Perhaps the most common control is a CommandButton. By itself, a CommandButton doesn't do anything, so you need to attach a macro to each CommandButton.

      Using dialog box controls directly in a worksheet often eliminates the need for custom dialog boxes. You can often greatly simplify the operation of a spreadsheet by adding a few ActiveX controls (or form controls) to a worksheet. These ActiveX controls let the user make choices by operating familiar controls rather than making entries in cells.

ActiveX Controls Form Controls
Excel versions 97, 2000, 2002, 2003, 2007, 2010, 2013, 2016, 2019 5, 95, 97, 2000, 2002, 2003, 2007, 2010, 2013, 2016, 2019
Controls available CheckBox, TextBox, CommandButton, OptionButton, ListBox, ComboBox, ToggleButton, Spin Button, ScrollBar, Label, Image (and others can be added) GroupBox,

Скачать книгу