PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Add VBA code to excel worksheet from vb.net-VBForums
Results 1 to 4 of 4

Thread: Add VBA code to excel worksheet from vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    2

    Add VBA code to excel worksheet from vb.net

    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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: Add VBA code to excel worksheet from vb.net

    oModule = oBook.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
    omodule needs o be set to the existing code module for the worksheet, not a new module

    try like
    vb Code:
    1. For Each cpane In ThisWorkbook.VBProject.VBE.CodePanes
    2.     If LCase(cpane.codemodule.Name) = "sheet1" Then Exit For
    3. Next
    4. cpane.codemodule.AddFromString(scode)
    change name of worksheet to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    2

    Re: Add VBA code to excel worksheet from vb.net

    Fixed. Thanks

  4. #4
    Junior Member
    Join Date
    Apr 2019
    Posts
    30

    Re: Add VBA code to excel worksheet from vb.net

    Quote Originally Posted by kaizen View Post
    Fixed. Thanks
    Please help me in putting vba code inside sheet1 and not in module1. Please I am begging.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width