Excel 高级编程:复制VBA代码到另一个文件
问题一:
一定要从testA()内复制部分的程式码到testB()吗?,如果是我要再试试?
这个做法很麻烦的,首先要判断testA()内的程式码msgbox是在第几行,总共要复制几行,然後再一行一行取出-插入,而testB()如果也不存在的话...........晕倒!
我先提供一个做法,先将要复制到testB()的程式码先写好,然後直接复制到
testB()的模组内
我的做法:
Sub CopySub()
Dim VBComp As VBComponent
Dim wbk As Workbook
'编写程式码
Code = ""
Code = Code & "Sub testb()" & vbCrLf
Code = Code & "Msgbox(""hi""), , ActiveWorkbook.Name" & vbCrLf
Code = Code & "End Sub" & vbCrLf
'新增Book(也可以用开启旧档)
Set wbk = Workbooks.Add
'新增Modlule,并命名
Set VBComp = wbk.VBProject.VBComponents.Add(vbext_ct_StdModule) '新增Module1
VBComp.Name = "NewModule"
Application.Visible = True
'写入程式码
With ActiveWorkbook.VBProject. _
VBComponents(VBComp.Name).CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
'立刻执行 Msgbox
Application.Run wbk.FullName & "!" & "testb"
End Sub