Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use a string to dynamically call subs

I tried something like:

sub tester()
   'do something
End Sub

activesheet.cells(1,1) = 'tester'

sub foo()
     dim x as string
     x = activesheet.cells(1,1).value
     Call x

End Sub

The intent would be to manipulate a cell so that as the cell value changed, the sub called would also change.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using Application.Run

  • Here’s an example with error handling to make it worth a while.
Option Explicit

Sub Test1()
    MsgBox "First"
End Sub

Sub Test2()
    MsgBox "Second"
End Sub

Sub Foo()
    On Error GoTo ClearError
    
    Dim S As String: S = CStr(Range("A1").Value)
    Application.Run S

ProcExit:
    Exit Sub
ClearError:
    MsgBox "Run-time error '" & Err.Number & ":" & vbLf & Err.Description
    Resume ProcExit
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading