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