XL97: Run-Time Errors Using UserForms Collection

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

SYMPTOMS

When you refer to the UserForms collection in a Visual Basic for Applications macro, you may receive one of the following error messages:

   Run-time error '13':
   Type mismatch

   -or-

   Run-time error '9':
   Subscript out of range

CAUSE

The UserForms collection is a collection of currently loaded UserForms; however, it does not provide its list as a property of the collection. Therefore, a statement such as UserForms(1).Show returns run-time error 9. In order to refer to an item in the UserForms collection, you must first add the UserForm list element to the UserForms collection.

WORKAROUND

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.

To refer to a UserForm within a UserForms collection and return a property or method for the UserForm, use either of the following methods.

Method 1: Create a UserForm Object

          The following subroutine displays a UserForm that already is
          created.

          Sub ShowForm()
              'Create an object called "x" to refer to UserForm1.
              set x = VBA.UserForms.Add("UserForm1")
              'Display the name of UserForm1.
              MsgBox x.Name
              'Show UserForm1.
              x.Show
          End Sub

Method 2: Refer Directly to the UserForm

          The following two statements dispaly a UserForm that already is
          created.

          UserForm1.Show

          -or-

          VBA.UserForms.Add("UserForm1").Show

Method 3: Reference the Item Property (index) of the UserForm

          The following subroutine displays a UserForm that already is
          created.

          Sub ShowForm()

              'Open UserForm1 into memory.
              Load UserForm1
              'Count the loaded UserForms and subtract one
              'because UserForm indexes start at zero.
              x = UserForms.Count - 1
              'Show UserForm1.
              UserForms.Items(x).Show

          End Sub

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

REFERENCES

For more information about the UserForms collection, click the Index tab in Visual Basic Help, type the following text

   UserForms collection

and then double-click the selected text to go to the "UserForm Object, UserForms Collection" topic.


Additional query words: XL97 8.00 dialogs
Keywords : kbcode kbprg xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbprb


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.