0

我在 VBA 编辑器(可扩展性)中编程并以编程方式创建一个新模块。新模块创建没有问题。但是,当创建新模块时,它会在代码窗格中作为一个空白模块打开,它就是这样。但是,我希望活动模块(运行创建新模块的代码的模块)在创建新模块后返回到代码窗格。我不知道执行此操作的命令是什么。这是我到目前为止所尝试的。

Sub addNewStandardModule()

On Error Resume Next'   Adds Extensibility Reference for programming in the VBE
ThisWorkbook.VBProject.References.AddFromGuid GUID:="{0002E157-0000-0000-C000-000000000046}", Major:=5, Minor:=3
On Error GoTo 0

Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule


        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
        VBComp.name = "Module6"

        '  This is my attempt to return the active module to the active codepane, but it doesn't work
        With Application.VBE
             If Not .ActiveCodePane Is Nothing Then
                    Set .ActiveVBProject = .ActiveCodePane.CodeModule.Parent.Collection.Parent
             End If
        End With

End Sub
4

1 回答 1

0

自己发现的。希望它将来对其他人有用。

Sub createNewCodeModuleAndReturnToThisOne()
Dim VBEProj As VBIDE.VBProject
Dim vbComp As VBIDE.VBComponent
Dim VBProj As VBIDE.VBProject


    Set VBEProj = ActiveWorkbook.VBProject
    Set vbComp = Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
    vbComp.name = "Module6"
    VBEProj.VBComponents("Module5").Activate
End Sub
于 2017-05-15T18:39:59.443 回答