mocnak
05-16-2014, 08:13 AM
hi, I am trying to add module with code to Sheet1 in Excel, and this module requires "Microsoft Visual Basic For Applications Extensibility 5.3" reference.
reference :
Public Sub reference()
ThisWorkbook.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 1, 0
'Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
End Sub
and module :
Public Sub module()
Dim CodeMod As VBIDE.CodeModule
Dim S As String
Set CodeMod = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule
S = _
"Private Sub Worksheet_SelectionChange(ByVal Target As Range)" & vbNewLine & _
"MsgBox (""hello world"")" & vbNewLine & _
"End Sub" & vbNewLine
With CodeMod
.InsertLines .CountOfLines + 1, S
End With
End Sub
it works OK when I run them separately, is it possible to merge these 2 macros ?
when I tried, I get error : "Compile Error : User defined type not defined", because it first recognize I use "VBIDE.CodeModule" reference, before it starts.
reference :
Public Sub reference()
ThisWorkbook.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 1, 0
'Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
End Sub
and module :
Public Sub module()
Dim CodeMod As VBIDE.CodeModule
Dim S As String
Set CodeMod = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule
S = _
"Private Sub Worksheet_SelectionChange(ByVal Target As Range)" & vbNewLine & _
"MsgBox (""hello world"")" & vbNewLine & _
"End Sub" & vbNewLine
With CodeMod
.InsertLines .CountOfLines + 1, S
End With
End Sub
it works OK when I run them separately, is it possible to merge these 2 macros ?
when I tried, I get error : "Compile Error : User defined type not defined", because it first recognize I use "VBIDE.CodeModule" reference, before it starts.