+ Reply to Thread
Results 1 to 9 of 9

Code runs on entire workbook...need it to only run on one worksheet

  1. #1
    Registered User
    Join Date
    11-02-2013
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Question Code runs on entire workbook...need it to only run on one worksheet

    Hey there,

    I am new to writing vba code and came across this problem. Seems pretty simple but I can't seem to get it to work. I am looking for the below code to work only on sheet1 of the workbook. Please help??

    Please Login or Register  to view this content.
    Last edited by alansidman; 11-25-2013 at 01:21 PM. Reason: code tags

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Code runs on entire workbook...need it to only run on one worksheet

    jrobertson,

    Welcome to the forum. Unfortunately your post does not conform to rule #3.

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button).

    To edit your previous submission click 'Edit Post' below post #1. Highlight your code and press the # in the toolbar.

    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

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

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Please use code tags when posting. Maybe:

    Please Login or Register  to view this content.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,379

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Hi,

    You need to learn about VBA code behind a worksheet vs behind a MODULE. If it is behind a single worksheet it can only function on that worksheet. It seems you have this code behind a module so it can work on any worksheet.

    Look at http://www.contextures.com/xlvba01.html for more of what I'm thinking you need to learn/understand.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,785

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added them for you today. Please read all forum rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  6. #6
    Registered User
    Join Date
    11-02-2013
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Thank you. I will look into that.

  7. #7
    Registered User
    Join Date
    11-02-2013
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Thank you for the response but it was still unsuccessful..


    Quote Originally Posted by Solus Rankin View Post
    jrobertson,

    Welcome to the forum. Unfortunately your post does not conform to rule #3.

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button).

    To edit your previous submission click 'Edit Post' below post #1. Highlight your code and press the # in the toolbar.

    Please Login or Register  to view this content.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,010

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Hi jrobertson2403. First, it isn't necessary to quote an entire post just to respond that it wasn't successful. It adds clutter to the thread.

    Can you clarify your question? That is, you said that you want this code to run only on one specific worksheet. The code provided by Solus Rankin does indeed run only on one Sheet1, but I'm not convinced that's what you really meant. Do you mean "run this code only when Sheet1 is being printed but not if any other sheet is being printed"?

    The Workbook_BeforePrint detects when any print event occurs anywhere in the workbook, but it cannot detect a print event for any specific worksheet. There may be some way to do it but I couldn't find it. The alternative would be to create a macro specifically to print Sheet1 and then also run this code, but then you (or the user) would have to use that macro rather than just hitting Print.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  9. #9
    Registered User
    Join Date
    11-02-2013
    Location
    Philadelphia,PA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Code runs on entire workbook...need it to only run on one worksheet

    Jeff-

    That is exactly what I am trying to do...just prevent printing on one page. I appreciate your help. Thank you!!

+ 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] Shortening Code so it runs if another workbook is acitve
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-17-2012, 06:47 AM
  2. Combobox code runs while closing and opening of workbook
    By shreyasdate in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2012, 07:47 AM
  3. worksheet combo box code runs when workbook open and close
    By hilander in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-29-2008, 10:25 PM
  4. Code runs in wrong workbook?
    By Ian in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-27-2005, 11:00 AM
  5. When code runs...worksheet acts a little strange
    By KimberlyC in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-01-2005, 12:06 PM

Tags for this Thread

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