XL98: Run-time Error Using Copy Method to Copy Module Sheet (189004)
The information in this article applies to:
- Microsoft Excel 98 Macintosh Edition
This article was previously published under Q189004 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
CAUSE
This problem may occur if the following conditions are true:
WORKAROUNDMicrosoft 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.
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.
- Start the Visual Basic Editor (press OPTION+F11).
- If Project Explorer is not visible, press COMMAND+R to activate Project Explorer.
- Click to select the Visual Basic project that contains the module sheet that you want to copy.
- On the Tools menu, click References. Click to select the Microsoft Visual Basic for Applications Extensibility check box and then click OK.
- On the Insert menu, click Module.
- 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
- 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.
| Modification Type: | Minor | Last Reviewed: | 10/10/2006 |
|---|
| Keywords: | kbdtacode kberrmsg kbnofix kbprb KB189004 |
|---|
|