Run-time error '9':
Subscript out of range
Subscript out of range
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.
View products that this article applies to.
ThisWorkbook.Sheets("Module2").Copy before:=ActiveWorkbook.Sheets(1)
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
Keywords: KB211604, kbprb, kbnofix, kberrmsg, kbdtacode