+ Reply to Thread
Results 1 to 8 of 8

How to run a worksheet specific macro only on one sheet

  1. #1
    Registered User
    Join Date
    01-08-2014
    Location
    Bath, UK
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    86

    How to run a worksheet specific macro only on one sheet

    This feels like a macro 101 question, so please don't laugh as I'm still relatively new to macros compared to most of you folks...

    I'm trying to add some code so that it only runs on the specific sheet or a range of specific sheets - but this list might change in the future) as a worksheet event... When I have been doing this in the past, I have been copying the code to each of the individual sheets but this now seems to be incorrect.

    This time, though, I have created the code on one sheet (with a Private Sub Worksheet_Calculate() event) which launches a macro in a separate module - my logic being to have as little code as possible in each of the sheets for copying, pasting, editing purposes, and centralise the main code in the module.
    However, I've noticed that this code is actually being run on all the sheets - even those I do not want it to run on. I thought, while developing this code, that it would only run on the one sheet it had been added to (e.g. right click the worksheet name, view code).

    I've clearly misunderstood something in my learning, copying code and adapting it from various posts.

    Can you help me, please!

    Thanks,
    Graham

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: How to run a worksheet specific macro only on one sheet

    Graham,
    Please share your workbook, & briefly describe what does it need to perform. I'm sure you'll get helpful responses from the forum to develop your VBA skills & improve the code.
    From the limited info you have currently provided one can understand your basic issue but can offer little help.

  3. #3
    Registered User
    Join Date
    01-08-2014
    Location
    Bath, UK
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    86

    Re: How to run a worksheet specific macro only on one sheet

    Thanks jewelsharma,

    The code I am trying to run on one sheet intitially while testing, then on several specific ones (but not all the sheets) is:

    Please Login or Register  to view this content.
    I presently have this code stored in one worksheet in my expectation that it would only work on that one sheet at the moment.


    The macro it refers to (which is stored in a module) is:

    Please Login or Register  to view this content.


    I am checking for inconsistent formula "errors" (e.g. where someone has added a value to a formula and the formula is now different to the adjacent ones) and colouring them (instead of conditional formatting, though I do have some conditional formatting on the worksheet too).

    At the moment, the macro is being run across all the sheets in the workbook, but I would only expect it to run on the individual sheet at the moment and then on a specified range of sheets in the near future. I may simply have misunderstood where I need to locate the code (i.e. "user error"), rather than it being a problem with the code itself.

    Any help would be appreciated!

    Thanks,
    Graham

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: How to run a worksheet specific macro only on one sheet

    Your macro Formula_error_colour() is coded to run on the ActivesSheet & hence it runs on any sheet that is active at the time. Worksheet_Calculate event, though specific to a worksheet is fired every time any cell on that recalculates. If your workbook is set to auto-calculate, this means it will fire every time anything recalculates, which basically means anytime anything changes in the workbook.
    As a fix, look to avoid using ActiveSheet, and use the individual workshhet name.

  5. #5
    Registered User
    Join Date
    01-08-2014
    Location
    Bath, UK
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    86

    Re: How to run a worksheet specific macro only on one sheet

    Thanks for the reply jewelsharma,

    I've moved the code all into the same worksheetto try to eliminate that error, so hopefully that works.
    (the majority of the sheet is protected, but this seemed to prevent the macro running, so I've included code to unprotect it, allow the macro to run, then protect it again - does this make sense, or do you think the macro should work even on a protected sheet?)


    Please Login or Register  to view this content.

    I'm still a little confused by
    As a fix, look to avoid using ActiveSheet, and use the individual workshhet name.
    as this would mean modifying the code in each worksheet specific to that particular worksheet. If that's the only way, then so be it, but I wondered whether there might be an alternative way - e.g. minimal code in the individual worksheet and one macro residing elsewhere (rather than having 60 or so macros to check and maintain - one for each cost centre). If I have a list of the cost centres in column E on "List of cost centres", do you know what code I would need to add to check against this, please?

    Thanks,
    Graham

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How to run a worksheet specific macro only on one sheet

    Maybe something like this?

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-08-2014
    Location
    Bath, UK
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    86

    Re: How to run a worksheet specific macro only on one sheet

    Thanks John,

    I've deleted the previous code from the worksheet it was in, pasted your code into "This Workbook" and specified 3 sheets to start with, but it still seemed to be running on all the sheets, rather than just the specified ones.

    Following jewelsharma's earlier comment, and basing it on your code, I've tried changing
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    . This seems to limit the code to the particular sheet (so thank you both of you), but now the macro stops at
    Please Login or Register  to view this content.
    (Run-time error 1004: Application-defined or object-defined error).
    When I've been testing the code prior to this post, it seemed to stop here if there wasn't actually an inconsistent formula on the sheet, but as I have deliberately entered an alteration to the formula, I'm surprised this is error is occurring.... any thoughts?

    Thanks,
    Graham

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How to run a worksheet specific macro only on one sheet

    Graham, I'm only taking a stab at it. Not familiar with that error check. Maybe:

    Please Login or Register  to view this content.

+ 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. Copying specific data to a specific field in a worksheet macro
    By bradpeh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2013, 01:52 AM
  2. Macro to unhide specific sheet based on worksheet name in cell.
    By CRIMEDOG in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2013, 08:56 PM
  3. VBA or macro to copy specific value to another worksheet from raw sheet
    By satputenandkumar0 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-28-2012, 05:05 AM
  4. Replies: 3
    Last Post: 11-28-2012, 05:02 AM
  5. Non-Sheet Specific Macro to copy range of cells to next worksheet in WB
    By Petrolcb41 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-29-2010, 11:04 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