Name:  vbforum.jpg
Views: 117
Size:  35.5 KB

Here is my code in creating command button in excel.

   Dim xlMod As Microsoft.Vbe.Interop.VBComponent
Dim objBtn As Object
        Dim celLeft As Integer
        Dim celTop As Integer
        Dim celWidth As Integer
        Dim celHeight As Integer

        celLeft = xlWorkSheet1.Range("d5").Left
        celTop = xlWorkSheet1.Range("d5").Top
        celWidth = xlWorkSheet1.Range("d5").Width
        celHeight = xlWorkSheet1.Range("d5").Height

        objBtn = xlWorkSheet1.OLEObjects.Add(ClassType:="Forms.Commandbutton.1", link:=False, _
        displayasicon:=False, Left:=celLeft, Top:=celTop, Width:=celWidth, Height:=celHeight)
        xlWorkSheet1.commandbutton1.caption = "Generate Sheet 2"
        xlWorkSheet1.commandbutton1.Height = 33
        xlWorkSheet1.commandbutton1.Width = 125
        xlWorkSheet1.commandbutton1.font.size = "12"

xlMod = xlWorkBook.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
        Dim codeString As New StringBuilder
        codeString.AppendLine("Public Sub SaySomething()")
        codeString.AppendLine("MsgBox ""Hello""")
        codeString.AppendLine("End Sub")
What I want is to insert code in Sheet1 and not adding module 1 to inset onto it.
Name:  vbforum2.PNG
Views: 89
Size:  4.1 KB