XL98: Run-Time Errors Using UserForms Collection (188564)



The information in this article applies to:

  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q188564

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 user forms; 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 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. To refer to a user form within a UserForms collection and return a property or method for the user form, use one of the following methods.

Method 1: Create a UserForm Object

The following subroutine displays a user form that has been created.
 Sub ShowForm()

      Dim x As Object

      '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 User Form

The following two statements display a user form that has been created:
   UserForm1.Show
				
-or-
   VBA.UserForms.Add("UserForm1").Show
				

Method 3: Reference the Item Property (index) of the User Form

The following subroutine displays a UserForm that has been created.
     Sub ShowForm()
      Dim x As Integer

      '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.Item(x).Show

   End Sub
				

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.

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.

Modification Type:MinorLast Reviewed:9/12/2006
Keywords:kbbug kbdtacode kberrmsg kbpending kbProgramming KB188564