+ Reply to Thread
Results 1 to 9 of 9

Using Application.Run to call a private sub in a module from ThisWorkbook object

  1. #1
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Using Application.Run to call a private sub in a module from ThisWorkbook object

    Hi all,

    Banging my head. I can't work out why this is not working.

    I have the following code behind the "ThisWorkbook" object
    Please Login or Register  to view this content.
    and some example code in sit in a normal module called SubsToCall
    Please Login or Register  to view this content.
    However, when running the event Workbook_Open as soon as it hits the first instance of .Run I get a '1004' runtime error "Application-defined or object-defined error".

    Why can I not 'Run' a sub from the ThisWorkbook object? I can 'Call' public and normal subs, but not private, and I'm fairly sure that I have used 'Run' from a sheet object before. Any ideas?
    Design everything to be as simple as possible, but no simpler.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    .
    It appears your macros are named : TestPublic, TestNormal, TestPrivate.

    Usually, macros are called from ThisWorkbook in this manner :

    Please Login or Register  to view this content.

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    You cannot call a private sub outside it's module. for the rest goes.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    Hi,

    Your code works for me, as expected. Is the workbook opening in Protected mode perhaps, and then being allowed? That causes many problems with Application calls, though usually the message is something like 'Method Run of the Application class failed.'
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    Quote Originally Posted by bakerman2 View Post
    You cannot call a private sub outside it's module
    You can with Run, actually- it ignores scope.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    The error arises when the name of the module is spelled wrong.
    So check the spelling of the module name in your WorkBook_Open.

  7. #7
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    Quote Originally Posted by Logit View Post
    Usually, macros are called from ThisWorkbook in this manner
    Quote Originally Posted by bakerman2 View Post
    for the rest goes.
    Hi, I've already stated that 'Call' works (with or without the 'Call' method) for public and normal subs.


    Quote Originally Posted by xlnitwit View Post
    Hi,

    Your code works for me, as expected. Is the workbook opening in Protected mode perhaps, and then being allowed? That causes many problems with Application calls, though usually the message is something like 'Method Run of the Application class failed.'
    No workbook protection. I unprotected every sheet in the workbook but got the same result. Thanks for testing for me.


    Quote Originally Posted by bakerman2 View Post
    The error arises when the name of the module is spelled wrong.
    So check the spelling of the module name in your WorkBook_Open.
    That was my first thought, so I have already double checked the spelling and copy/pasted the module name into the .Run parameter.

    However, to eliminate the module name / spelling as a factor I renamed the module to "m" and it worked as expected. I then tried to following to find out what was throwing a spanner in the works:

    Please Login or Register  to view this content.
    So it seems that the keyword "sub" is being parsed by the .Run method in a certain (unexpected in this case) way. I've renamed my module to "ProcsToCall" and everything works as expected. I use the module name "SubsToCall" quite a lot so I will have to remember to change all of them to avoid this error in the future.

    Thanks everybody for all your help moving towards the solution, I appreciate it!

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    Glad you got it working. Thanks for rep+.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Using Application.Run to call a private sub in a module from ThisWorkbook object

    Quote Originally Posted by Stormin' View Post
    So it seems that the keyword "sub" is being parsed by the .Run method in a certain (unexpected in this case) way.
    That is very strange indeed. It works fine here with Excel 2010. Purely out of interest, does it work adding the workbook name thus
    Please Login or Register  to view this content.
    Thank you for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Call Private Sub from a General Module
    By aquixano in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-01-2016, 09:46 AM
  2. Call Specific worksheet function from ThisWorkbook module
    By losekiss431a in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-21-2015, 05:17 AM
  3. [SOLVED] Problem with ThisWorkbook Module: Two Private Sub Workbook_Open()
    By DavidRoger in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-22-2015, 11:28 AM
  4. How to call a procedure from a different module, witihin "ThisWorkbook"
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-08-2012, 09:59 AM
  5. Replies: 1
    Last Post: 08-30-2011, 02:23 AM
  6. call private sub from module
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2009, 10:02 AM
  7. call module sub from sheet private sub
    By tango in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2009, 03:10 AM

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