+ Reply to Thread
Results 1 to 3 of 3

How to Reference the Worksheet that VBA Code is Running In?

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    221

    How to Reference the Worksheet that VBA Code is Running In?

    I have a workbook with multiple sheets. I want to add macro code to each sheet, that runs (and makes changes to that sheet) when a macro button on the sheet is clicked. Theoretically, then, if I create a copy of one of the sheets, the new copy will contain the same macro code, which will run on THAT sheet when its macro button is clicked.

    I could use ThisWorkbook.ActiveSheet to refer to the worksheet itself (in each of the macros), since logically the sheet should be active when its macro button is clicked. But someone COULD run the macro (from within the macro editor) when some OTHER sheet is actually active. So I'd like to find a way to assign a worksheet variable (maybe called ThisSheet) to the worksheet where the macro code that's executing is located. Is there any simple way to do this?

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to Reference the Worksheet that VBA Code is Running In?

    When you put event code in a worksheet, the title line starts with the word 'Private' which means it will run in the host sheet only and cannot be called like a standard Sub procedure that is installed in module1 or any other Class code module. You do not have to use ActiveSheet in the code to refer to the host sheet, you can just use the constant 'Me', which in VBA refers to the object hosting the code. eg. for a textbox value on a sheet running event code
    Please Login or Register  to view this content.
    would put "Hello" into textbox 1. Ranges and Cells references automatically relate to the host sheet, unless otherwise specified.. In other words, you only need the sheet reference to refer to controls or other sheets. Excel knows that any range reference that is not qualified with a different sheet reference belongs to the sheet running the code.
    Last edited by JLGWhiz; 09-05-2019 at 08:21 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,795

    Re: How to Reference the Worksheet that VBA Code is Running In?

    If you put the code in the sheet module you can use the Me keyword like
    Please Login or Register  to view this content.
    where Me then refers to that specific sheet

+ 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 to hide/unhide rows running very slow in worksheet
    By alicia. in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-17-2017, 01:21 PM
  2. [SOLVED] How to prevent worksheet code from running while Module Macro is running:
    By Chad Bateman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2015, 09:26 AM
  3. Code for Organizing a worksheet by rows: Not running
    By cwalker6 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2009, 07:07 PM
  4. Macro creating new worksheet. Is it possible to write running code for sheet
    By Kernal87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-30-2008, 06:17 AM
  5. how to prevent code running when in a worksheet code
    By Corey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-13-2006, 04:00 AM
  6. Running VBA Code not written within the currrent worksheet
    By DMB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2005, 01:05 PM
  7. [SOLVED] Altering code to reference the worksheet before the active worksheet
    By KimberlyC in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-15-2005, 07:06 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