+ Reply to Thread
Results 1 to 6 of 6

How do I run code in a specific sheet only?

  1. #1
    Registered User
    Join Date
    02-10-2020
    Location
    bay area
    MS-Off Ver
    10
    Posts
    5

    How do I run code in a specific sheet only?

    Let's say I have a Workbook with Sheets 1 to 10. I came up with the following and it works well enough since all the cells I want referenced are the same in each sheet. Except for Sheet 1.

    If my Sheet 1 cells are a2, b2, and c2 instead, how do I set it up to only run in Sheet 1?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    temp = Range("a1").Value
    temp = temp & Chr(13) & Range("b1").Value
    temp = temp & Chr(13) & "Week Ending" & " " & Range("c1").Value
    ActiveSheet.PageSetup.CenterHeader = temp

    End Sub
    Ideally I want to adjust it so that when I press save in any sheet, it will run the macro through all the sheets. I still haven't figured out how to do that.

  2. #2
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: How do I run code in a specific sheet only?

    Please Login or Register  to view this content.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: How do I run code in a specific sheet only?

    Quote Originally Posted by blu112 View Post
    Let's say I have a Workbook with Sheets 1 to 10. I came up with the following and it works well enough since all the cells I want referenced are the same in each sheet. Except for Sheet 1.

    If my Sheet 1 cells are a2, b2, and c2 instead, how do I set it up to only run in Sheet 1?
    Ideally I want to adjust it so that when I press save in any sheet, it will run the macro through all the sheets. I still haven't figured out how to do that.
    See if this is how you wanted.
    Please Login or Register  to view this content.
    Edit; added ws. for each Range in array.
    Last edited by jindon; 02-11-2020 at 12:17 AM.

  4. #4
    Registered User
    Join Date
    02-10-2020
    Location
    bay area
    MS-Off Ver
    10
    Posts
    5

    Re: How do I run code in a specific sheet only?

    Quote Originally Posted by jindon View Post
    See if this is how you wanted.
    Please Login or Register  to view this content.
    Edit; added ws. for each Range in array.
    This works perfectly. Thank you so much.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: How do I run code in a specific sheet only?

    You are welcome and thanks for the rep.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  6. #6
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,446

    Re: How do I run code in a specific sheet only?

    Administrative Note:

    Please don't quote entire posts unnecessarily. They clutter threads and make them hard to read.
    Use the "Quick reply" instead
    Thanks

+ 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. VBA code to only allow specific users to view specific rows in the same sheet
    By krunk in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 08-28-2023, 05:34 AM
  2. Require VBA code to Transfer specific data from one sheet to another sheet.
    By xlhelp7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-13-2017, 10:20 AM
  3. [SOLVED] VBA code to count offline leads from one sheet to another sheet with specific date
    By xlhelp7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-04-2016, 11:25 PM
  4. VBA Code to transpose data from Sheet 1 to Sheet 2 in a specific order
    By zicitron in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-24-2014, 07:20 AM
  5. Replies: 5
    Last Post: 12-12-2013, 02:53 PM
  6. [SOLVED] How to get (range)macro to always run code on a specific sheet regardless of active sheet?
    By JTwrk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2012, 04:33 PM
  7. Excel 2007 : Changing code to not be sheet specific
    By MiserableLawStdnt in forum Excel General
    Replies: 2
    Last Post: 10-10-2011, 09:41 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