HI All,
I have a vb.net program that I need to add code to an excel worksheet (not a macro)
I curently use:
Code:
' Create a new VBA code module.
oModule = oBook.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
Dim sCode As String = "Private Sub Worksheet_Change(ByVal Target As Range)" & Environment.NewLine
sCode = sCode & "If Range(""I4"").Value = ""Sell"" Then" & Environment.NewLine
sCode = sCode & "MsgBox(""Trade:"" & "" "" & Range(""D4"").Value & "" "" & Range(""A4"").Value)" & Environment.NewLine
sCode = sCode & "End If" & Environment.NewLine
sCode = sCode & "End Sub"
'Add the VBA macro to the new code module.
oModule.CodeModule.AddFromString(sCode)
But when I use this it adds the code to a macro in the excel file. and never gets called when the workseet changes.
As it uses The worksheet_Change event I need it to go into sheet1.
Any ideas?
Thanks
James