XL97: Using the New Keyword to Create Instances of a UserForm

Last reviewed: March 13, 1998
Article ID: Q167180
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

In Microsoft Excel 97, you can use the New keyword to create multiple instances of a particular class. This article includes sample Visual Basic for Applications code that uses the New keyword to create multiple instances of a UserForm. The new instances of the UserForm are copies of the original UserForm and can be controlled independently of the original UserForm.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Creating the Sample UserForm

To create the sample UserForm, do the following:

  1. Close all open workbooks, and then create a new workbook.

  2. Start the Visual Basic Editor (press ALT+F11).

  3. On the Insert menu, click UserForm.

  4. Add a ListBox control to the lower-left portion of the UserForm.

  5. Add a Label control above the ListBox control and change the Caption property to "Click an item" (without the quotation marks).

  6. Add three CommandButton controls to the right of the ListBox control.

  7. Use the following properties for the command buttons:

          Top Command Button:
    

             Property    Value
             -------------------------------
    
             Name        cmdNewForm
             Caption     create new instance
    
          Middle Command Button:
    
             Property    Value
             ----------------------------
    
             Name        cmdFormCaption
             Caption     UserForm caption
    
          Bottom Command Button:
    
             Property    Value
             ----------------------
    
             Name        cmdClose
             Caption     close form
    
    
    

Entering Macro Code

To create the macro, do the following:

  1. On the Insert menu, click Module.

  2. Enter the following code in the General Declarations section of the new module:

          Option Base 1
          ' Counter for instances of UserForms.
          Public mycount As Integer
    

          ' Array containing UserForm objects
          Public MyForms() As UserForm1
    

          Sub New_UserForms()
    
             ' Display the UserForm.
             UserForm1.Show
    
          End Sub
    
    

  3. Double-click the top command button and enter the following code for the Click event for the cmdNewForm command button:

          Private Sub cmdNewForm_Click()
    

             mycount = mycount + 1
    
             ' Increase the size of the MyForms array by one.
             ReDim Preserve MyForms(mycount)
    
             ' Create a new instance that is of type UserForm1.
             Set MyForms(mycount) = New UserForm1
    
             ' Set the caption of the UserForm to the instance number.
             MyForms(mycount).Caption = "instance " & mycount
    
             MyForms(mycount).cmdClose.Caption = "hide form"
    
             ' Add new item to the list box.
             UserForm1.ListBox1.AddItem mycount
    
          End Sub
    
    

  4. Double-click the middle command button and enter the following code for the Click event for the cmdFormCaption command button:

          Private Sub cmdFormCaption_click()
    

             ' Display the Caption of the current UserForm.
             MsgBox Me.Caption
    
          End Sub
    
    

  5. Double-click the bottom command button and enter the following code for the Click event for the cmdClose command button:

          Private Sub cmdClose_Click()
    

             ' Hide the current instance of the UserForm.
             Me.Hide
    
          End Sub
    
    

  6. Double-click the ListBox control and enter the following code for the MouseUp event:

          Private Sub ListBox1_MouseUp _
          (ByVal Button As Integer, ByVal Shift As Integer, _
          ByVal X As Single, ByVal Y As Single)
    

             ' Display the selected instance of the UserForm.
             MyForms(UserForm1.ListBox1.ListIndex + 1).Show
    
          End Sub
    
    

  7. Enter the following code for the QueryClose event for UserForm1:

          Private Sub UserForm_QueryClose _
          (Cancel As Integer, CloseMode As Integer)
    

             On Error Resume Next ' Ignore if no forms exist.
    
             ' Setting the value of Cancel to any integer other than 0 prevents
             ' you from clicking the Close ("X") button to close the UserForm.
             Cancel = 1
    
          End Sub
    
    

Running the Macros

To run the example macros, do the following:

  1. Run the New_UserForms macro.

    This step displays the UserForm.

  2. Click the Create New Instance command button.

    Each time you click the Create New Instance command button, a new instance of the UserForm is created, and a new item is added to the list box.

  3. Click any number in the list box.

    The relevant instance of the UserForm is displayed.

  4. Click the UserForm Caption command button.

    A message box displays the caption for the currently visible UserForm.

  5. Click the Hide Form command button.

    The currently visible UserForm is hidden, and UserForm1 is displayed.

    NOTE: The caption on the bottom command button was programmatically changed to Hide Form to differentiate it from the bottom button on UserForm1. When you hide one of the created instances of the UserForm, and UserForm1 is active, the caption on the bottom command button is "Close Form," which is the value set at design time.

  6. If UserForm1 is active, click the Close Form button to stop the macro.

REFERENCES

For more information about using the New keyword, click the Office Assistant in the Visual Basic Editor, type "dim" (without the quotation marks), click Search, and then click to view the "Dim Statement" topic.

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Excel Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office Program or
               Component


Additional query words: XL97
Keywords : kbprg kbhowto
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbinfo


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: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.