Excel 高级编程:对VBA编程环境(VBE)编程
Sub Addmod()
Dim VBComp As VBComponent
Dim Code As String
'新增Modlule,并命名
Set VBComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule) '新增Module1
VBComp.Name = "NewModule"
Application.Visible = True
'写入程式码
Each rng In Range("A1:A3")
Code = Code & rng & vbCrLf
Next rng
With ActiveWorkbook.VBProject. _
VBComponents(VBComp.Name).CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
End Sub