How to Create Next and Previous Buttons in a Custom Dialog Box

Last reviewed: December 1, 1997
Article ID: Q141566
The information in this article applies to:
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for Windows, version 5.0
  • Microsoft Excel for the Macintosh, version 5.0

SUMMARY

In Microsoft Excel, you can create buttons with the text "Previous" and "Next" that allow you to move between custom dialog boxes.

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 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 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/default.asp

The following steps show you how to set up four dialog boxes with Next and Previous buttons.

  1. Enter the following into a Visual Basic module sheet:

          Public x As Integer
    

          Sub ShowDialog()
             ' Start with Dialog two.
             x = 2
             Do
                ' Show the Dialog; if Cancel is pressed, exit the Do Loop.
                If DialogSheets(x).Show = False Then Exit Do
             Loop
          End Sub
    
          Sub PreviousDialog()
             'Hide the current dialog
             DialogSheets(x).Hide
             ' Sets the index to one previous.
             x = x - 1
             ' Test if we are going to show the first dialog box.
             If x = 1 Then
                ' If it is the first dialog box, then disable the Previous 
                ' button.
                DialogSheets(x).Buttons("Previous").Enabled = False
             End If
          End Sub
    
          Sub NextDialog()
             ' Hide the current dialog box.
             DialogSheets(x).Hide
             ' Sets the index to one next.
             x = x + 1
             ' Test if we are going to show the last dialog box.
             If x = DialogSheets.Count Then
                ' If it is the first dialog box, then disable the Next button.
                DialogSheets(x).Buttons("Next").Enabled = False
             End If
          End Sub
    
    

  2. Add a new dialog sheet.

  3. Delete the OK button.

  4. Add two new buttons. Change the caption and name of one to "Previous" and the other to "Next."

  5. Assign the "PreviousDialog" macro to the "Previous" button, and assign the "NextDialog" macro to the "Next" button.

  6. Copy the dialog box to create three more dialog boxes (total of four for the example).

    NOTE: A quick way to do this is to click Move Or Copy Sheet on the Edit menu and make copies of the entire dialog sheet.

  7. Starting from the dialog sheet farthest to the left, change the dialog boxes caption to "One," "Two," "Three," and "Four" (so you can tell when the dialog boxes change).

  8. Run the "ShowDialog" macro, click the Previous and Next buttons to move between the dialog boxes. The Cancel button will stop the macro.


Additional query words: 5.00 7.00
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,7.0; MACINTOSH:5.0
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 1, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.