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: Error Message: Run-time Error '9': Subscript out of Range


View products that this article applies to.

Symptoms

When you attempt to copy a Microsoft Visual Basic for Applications module sheet in a Visual Basic macro procedure, you may receive the following error message:
Run-time error '9':
Subscript out of range

↑ Back to the top


Cause

This problem may occur if the following conditions are true:
  • You are using the Copy method of the Sheets collection to copy a Visual Basic module sheet. Your line of code may be similar to the following:
    ThisWorkbook.Sheets("Module2").Copy before:=ActiveWorkbook.Sheets(1)
    					
  • The module sheet that you are trying to copy was inserted manually in Microsoft Excel.

↑ Back to the top


Workaround

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 professionals 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 a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS The following sample macro illustrates how to copy the contents of a module to a new code module contained in another workbook. This example assumes that you have a workbook open that contains two modules.
  1. Start the Visual Basic Editor (press ALT+F11).
  2. If Project Explorer is not visible, press CTRL+R to activate Project Explorer.
  3. Click to select the Visual Basic project that contains the module sheet that you want to copy.
  4. On the Tools menu, click References. Click to select the Microsoft Visual Basic for Applications Extensibility 5.3 check box, and then click OK.
  5. Click Module on the Insert menu.
  6. Type the following code into the module sheet:
       Sub CopyModule()
          Dim CodeLines As String
          Dim ModuleToCopy As VBComponent
          Dim NewModule As VBComponent
    
          ' Set a variable to the module to copy.
          ' Note: This assumes that the name of the module to copy is
          ' "module2". Replace "Module2" with the name of the module to copy.
          Set ModuleToCopy = _
                 Application.VBE.ActiveVBProject.VBComponents("module2")
    
          ' Get the Visual Basic code from the module.
          CodeLines = ModuleToCopy.CodeModule.Lines _
                     (1, ModuleToCopy.CodeModule.CountOfLines)
    
          ' Create a module sheet in another workbook.
          ' Note: You must refer to the name of the Visual Basic project. By
          ' default, the name of all new projects is "VBAProject". You may want
          ' to rename the project to which you are copying the contents of the
          ' module sheet.
          Set NewModule = Application.VBE.VBProjects("VBAProject") _
                          .VBComponents.Add(vbext_ct_StdModule)
    
          ' Add the Visual Basic code to the new module.
          NewModule.CodeModule.AddFromString CodeLines
    
          ' Rename the new module to the name of the previous module.
          NewModule.Name = ModuleToCopy.Name
    
       End Sub
    					
  7. Run the CopyModule procedure.
Excel will insert a new module sheet into the Visual Basic project named VBAProject, and will copy the code from the module named Module2 in the active Visual Basic project.

For additional information about manual insertion of modules, click the article number below to view the article in the Microsoft Knowledge Base:
213726 XL97: Modules Inserted Manually Are Not in Modules Collection

↑ Back to the top


Keywords: KB211604, kbprb, kbnofix, kberrmsg, kbdtacode

↑ Back to the top

Article Info
Article ID : 211604
Revision : 7
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 319