+ Reply to Thread
Results 1 to 7 of 7

Passing subroutine as a parameter

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-25-2008
    Location
    texas
    Posts
    178

    Passing subroutine as a parameter

    I want to pass the name of the routine as a parameter.

    sub  macro1(a,b)
    run a(b)
    end sub
    
    'which should run subroutine "a" with the parameter of "b".
    
    sub macro2(c)
    end sub
    so

    the code

    run macro1("macro2",7)
    should run macro 2 with the parameter of 7.




    I get errors every time I try it (with "calls", "runs",...)
    Last edited by jartzh; 03-26-2009 at 03:39 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Passing subroutine as a parameter

    works for me

    Sub Macro2(c)
        
        MsgBox "Macro2 " & c
        
    End Sub
    
    Sub Macro1(a, b)
    
        Application.Run a, b
        
    End Sub
    
    Sub test()
        Application.Run "macro1", "macro2", 7
    End Sub
    Last edited by Andy Pope; 03-26-2009 at 12:05 PM. Reason: corrected ommission in code
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-02-2004
    MS-Off Ver
    2007
    Posts
    43

    Re: Passing subroutine as a parameter

    Andy, did you mean:

    Sub test()
        Application.Run "Macro1", "macro2", 7
    End Sub
    or you could use

    Sub test()
        Macro1 "macro2", 7
    End Sub
    TJ

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Passing subroutine as a parameter

    Indeed I did
    Thanks for the correction

  5. #5
    Forum Contributor
    Join Date
    07-25-2008
    Location
    texas
    Posts
    178

    Re: Passing subroutine as a parameter

    I am still getting errors:

    My code is
    Sub day1move()
     rundailyloadroutine "loadoverroutine", 7
    End Sub
    
    Sub rundailyloadroutine(a, b)
     Run a(b)
    End Sub
    
    Sub loadoverroutine()
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub
    but I get a "runtime error 13 type mismatch on the "run a(b).
    and a "compile error. expected sub,function,or property" fif I try call a(b)

    Thanks guys

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Passing subroutine as a parameter

    loadoverroutine had no argument which meant even after correcting the Run command it would still fail.

    Either add the argument to the loadoverroutine routine header or
    remove b from the Run command.

    Sub rundailyloadroutine(a, b)
     Run a, b
    End Sub
    
    Sub loadoverroutine(b)
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1