Microsoft KB Archive/111563

From BetaArchive Wiki
Knowledge Base


Sample Macro to Add Check Marks to Custom Menu Commands

Article ID: 111563

Article Last Modified on 6/11/2007



APPLIES TO

  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Excel 5.0 for Macintosh



This article was previously published under Q111563

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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. 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: howto XL

Keywords: kbcode kbhowto kbprogramming KB111563