+ Reply to Thread
Results 1 to 8 of 8

Can/should you put Worksheet code into a standard module?

  1. #1
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Can/should you put Worksheet code into a standard module?

    I have a workbook with 11 worksheets with the exact same format with Worsheet_Change and Worksheet_SelectionChange vba behind it. After I originally created the first worksheet, I simply copied it to the remaining ten worksheets. I’m making some changes to that code now and I wondered if it was possible to move the code to a standard module and call it from the individual worksheets? That way I only have to change the code in one place. The worksheet code needs to identify the: 1) row, 2) column, 3) value, and 4) range name of the selected/changed range. Questions:

    - Is this a bad idea / what are the trade-offs for this plan?
    - As long as I fully qualify the Target, I believe the code should work in a standard module. Does anyone see anything else I would need for this plan?

    Finally, I noticed my change event is: Private Sub Worksheet_Change(ByVal Target As Excel.Range) – probably because I found some of the code on the internet. Using the dropdown to create it new the code is: Private Sub Worksheet_Change(ByVal Private Sub Worksheet_Change(ByVal Target As Range)). What is the significant of “Target As Range” vs. “Target As Excel.Range?”

    Thanks for reading and any help

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Can/should you put Worksheet code into a standard module?

    I never use the same code on multiple sheets.

    Your plan is good.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Can/should you put Worksheet code into a standard module?

    Thanks for the feedback!

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Can/should you put Worksheet code into a standard module?

    Hi aquixano

    If indeed all of your Worksheets are formatted the same there are, in ThisWorkbook Module, these available options...

    Please Login or Register  to view this content.
    and...
    Please Login or Register  to view this content.
    If they're NOT formatted the same and you wish to exclude one or several this is easily achieved.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Can/should you put Worksheet code into a standard module?

    Interesting idea jaslake – I wouldn’t have thought of that. Actually the 11 I mentioned are the same, three others are the identical to each other and the remaining half dozen are unique. A select case on sh.name will work to exclude worksheets as necessary. Is that the best way to do that? Thanks.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Can/should you put Worksheet code into a standard module?

    Hi aquixano

    Select Case will work or ...
    Please Login or Register  to view this content.
    Sounds like you have the general idea...need additional help let me know...

  7. #7
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Can/should you put Worksheet code into a standard module?

    I'm with you - Thanks!

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Can/should you put Worksheet code into a standard module?

    You're welcome...glad I could help. Thanks 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. [SOLVED] Code locks cells when inserted in sheet module but returns error in standard module
    By yoda66 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 07:39 AM
  2. [SOLVED] Why do I get an error when running the code for an UserForm from a standard module?
    By kjy1989 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-24-2013, 01:50 AM
  3. userform event handling within a standard code module?
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2010, 01:01 PM
  4. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM
  5. [SOLVED] copying vba code to a standard code module
    By 1vagrowr in forum Excel General
    Replies: 2
    Last Post: 11-23-2005, 12:10 PM
  6. [SOLVED] Running Standard Module Code from Dataform
    By Jim May in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2005, 12:05 PM
  7. Run worksheet module code from workbook module?
    By keithb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2005, 12:05 AM
  8. Create a newworksheet with VBA code and put VBA code in the new worksheet module
    By ceshelman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2005, 12:05 PM

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