Call VBA sub using string value

This is my test code.

Sub dotask() Dim qusub As String qusub = Worksheets("Task List").Range("C2").Value MsgBox qusub Application.Run qusub End Sub Sub msg1() MsgBox "sub msg1" End Sub Sub msg2() MsgBox "sub msg2" End Sub Sub msg3() MsgBox "sub msg3" End Sub Sub msg4() MsgBox "sub msg4" End Sub 

All this is contained in one standard module. I read Attempting to call Sub using String - VBA and wrote my code according to what I found there (e.g. using Application.Run). Table C2 of the task list contains "msg3" at the moment. When I execute sub "dotask", I first get a window with the message "msg3" as I want, but then I get the following error message:

Runtime Error '1004':

Cannot start macro "msg3". A macro may not be available in this book, or all macros may be disabled.

I am working on Excel 2010, and the .xlsm file - what should I do to get my code to execute as I want?

+6
source share
2 answers

just launched it here. msg1 seems like a reserved word ... changes it to something else, and it works fine =)

+3
source

Using GetRef, you give a link to sub. See my question here for example

EDIT: following the suggestions in the comments, here is part of the solution to this issue.

 sub one(para) WScript.Echo para & " from one" end sub sub two(para) WScript.Echo para & " from two" end sub sub main(subname, para) Dim f : Set f = GetRef(subname) f para end sub main "one", "test" '=>test from one 
+1
source

Source: https://habr.com/ru/post/988838/


All Articles