1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Sub test()
Dim a As Integer
Dim code As String
code = "Sub Test()" & vbCrLf
code = code & "dim a,i" & vbCrLf
code = code & "for i = 1 to 10" & vbCrLf
code = code & "a = a + 10" & vbCrLf
code = code & "next " & vbCrLf
code = code & "msgbox a" & vbCrLf
code = code & "End Sub" & vbCrLf
Set scr = CreateObject("ScriptControl")
scr.Language = "VbScript"
scr.AddObject "This", ThisWorkbook, True
Set m = scr.Modules.Add("Module1")
m.AddCode code
m.Run "test"
Set m= nothing
Set scr = nothing
End Sub |
Partager