Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Run-Time Errors Using UserForms Collection


View products that this article applies to.

Symptoms

When you refer to the UserForms collection in a Microsoft 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

↑ Back to the top


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.

↑ Back to the top


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 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 display 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.Item(x).Show
   End Sub
				

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


References

For more information about UserForms collection, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type UserForm Object, UserForms Collection in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Article Info
Article ID : 207714
Revision : 5
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 345