Results 1 to 2 of 2

Thread: Insert Macro in excel

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Insert Macro in excel

    Hi

    How can I to insert a macro inside Excel using VB, I need to Insert Macro and to Call It

    Is possible to do ?

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Insert Macro in excel

    Code:
    Sub Sample()
        Dim xlapp As Object, xlbook As Object, xlmodule As Object
        Dim strCode As String
           
        Set xlapp = CreateObject("Excel.Application")
        
        xlapp.Visible = True
    
        Set xlbook = xlapp.Workbooks.Add
        Set xlmodule = xlbook.VBProject.VBComponents.Add(1)
    
        '~~> Add a macro to the module...
        strCode = "Sub MutelyMacro()" & vbCr & _
                  "MsgBox ""Hello Mutely. You have successfully created the macro :-)"" " & vbCr & _
                  "End Bub"
        
        xlmodule.CodeModule.AddFromString strCode
    
        '~~> This is to run that macro
        xlapp.Run "MutelyMacro"
    
        '~~> Cleanup
        Set xlmodule = Nothing
        
        '~~> Remove the below line if you don't want to save it...
        xlbook.Saved = True
        xlapp.Quit
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width