XL: Sample Macro to Add Check Marks to Custom Menu Commands

ID: Q111563

The information in this article applies to:

SUMMARY

The following Microsoft Excel macro shows how to manage check marks in a custom menu command. The macro places a check mark next to the command when it is selected from the menu. When the command is selected again, the macro removes the check mark.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

Follow these steps to create and run the sample macro code:

1. Insert a new module in a workbook.

2. To add a command called "Database" below the Toolbars command on the

   View menu, choose the Menu Editor command from the Tools menu.

   For more information about using the Menu Editor, see pages 242-244 of
   the "Visual Basic User's Guide."

3. Select a portion of the worksheet that you want to be the database view
   and give it the assigned name "Database_View." Repeat this procedure for
   the worksheet view, and give that worksheet selection the defined name
   "Worksheet_View."

4. Switch to the Visual Basic module, and enter the following code:

      Sub DatabaseView()
          With MenuBars(xlWorksheet).Menus("View").MenuItems("Database")
              .Checked = Not .Checked 'toggles check mark on/off
              If .Checked Then
                  'Switch to Database View
                  Application.Goto "Database_View", True
              Else
                  'Switch to Worksheet View
                  Application.Goto "Worksheet_View", True
              End If
          End With
      End Sub

5. Using the Menu Editor, assign the DatabaseView macro to the Database
   command.

6. Activate the worksheet.

7. Choose Database from the View menu.

The macro will toggle the check mark on or off. When Database is selected, the macro scrolls the window to a cell range where a database is entered. If Database is not selected, the macro scrolls the window to a cell range where you can enter worksheet data.

REFERENCES

"Visual Basic User's Guide," version 5.0, page 254

Additional query words: 5.00 howto

Keywords          : kbcode kbprg PgmHowto 
Version           : WIDNOWS:5.0,7.0; MACINTOSH:5.0
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999