+ Reply to Thread
Results 1 to 5 of 5

Problem calling routine from a class in an add-in

  1. #1
    Registered User
    Join Date
    01-10-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    77

    Problem calling routine from a class in an add-in

    Hi,

    I've got three workbooks that end users will be using for data entry. The VBA code for the three workbooks is identical.

    I'm trying to encapsulate the code into an add-in so I only have one copy of the code that I have to debug/maintain.

    Furthermore, I've also encapsulated the code in the add-in into a class. The class isn't really a "usual" object modelling some data; rather, it's a utility class processing Worksheet_Change and Worksheet_Activate events. Hopefully this isn't a really bad idea, rather than keeping the code in standard modules in the add-in. Let me know if this is particularly bad design.

    The application is called "Midas". Here are some code excerpts:

    Midas.xlam:

    Standard Module:

    Please Login or Register  to view this content.
    Class Module:

    Please Login or Register  to view this content.
    And in the end user workbook:

    Please Login or Register  to view this content.
    The Wbk_SheetChange event handler is triggering, but it's looking for the code in the end user workbook, instead of the class. Is there a way I can change

    Please Login or Register  to view this content.
    to call the private routine "Cases_Changed" (where "Cases" is the sheetname) residing in the class instead of looking for it in the workbook?

    Thanks,
    Scott

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Problem calling routine from a class in an add-in

    I do not understand this arrangement at all. You have consolidated and encapsulated your code in one workbook and one class, but then created methods that are named after specific sheets in other workbooks, so destroying the encapsulation. Why must you have routines named for each sheet? (if you must, you may use CallByName if the methods are public)
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    01-10-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    77

    Re: Problem calling routine from a class in an add-in

    I'm happy to design this better. The code needs to do different things in different worksheets.

    I think I could just do this, but it's klunky:

    Please Login or Register  to view this content.
    More code, apologies for the length:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-10-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    77

    Re: Problem calling routine from a class in an add-in

    P.S.: I'd prefer to keep the class methods private. And I'm open to someone saying using a class in this way is a bad idea. Although I'm not sure putting the code in standard modules in the add-in would fix this issue.

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Problem calling routine from a class in an add-in

    If the code is in standard module then you may use application.run so it will be easy fix.

    If your code will be different for each sheet it may be preferable to use Worksheet variables declared WithEvents - I do not see any benefit with a workbook level event for different codes.

+ 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. [SOLVED] VBA Calling Another Sub Routine to Run
    By mowens74 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-15-2013, 12:34 PM
  2. Calling routine with name stored in string variable
    By Helge in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2011, 12:07 PM
  3. calling a class by name
    By 427v8 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2008, 12:46 AM
  4. Calling Routine by Variable Name
    By Caligula in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2008, 11:44 PM
  5. [SOLVED] Read directly a returned array in the calling routine
    By Jean-Pierre Bidon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-14-2005, 09:45 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