How to run a string as a command in VBA

前端 未结 2 2075
终归单人心
终归单人心 2020-12-03 12:23

I have this simple VBA code below, and I don\'t know why is not working.

Sub Run()
    test = \"MsgBox\" & \"\"\"\" & \"Job Done!\" & \"\"\"\"
           


        
2条回答
  •  一个人的身影
    2020-12-03 12:39

    You may be tempted by adding your own string "Executer":

    Sub StringExecute(s As String)
        Dim vbComp As Object
        Set vbComp = ThisWorkbook.VBProject.VBComponents.Add(1)
        vbComp.CodeModule.AddFromString "Sub foo()" & vbCrLf & s & vbCrLf & "End Sub"
        Application.Run vbComp.name & ".foo"
        ThisWorkbook.VBProject.VBComponents.Remove vbComp
    End Sub
    
    Sub Testing()
        StringExecute "MsgBox" & """" & "Job Done!" & """"
    End Sub
    

提交回复
热议问题