+ Reply to Thread
Results 1 to 4 of 4

Worksheet_Activate () - Need to work when any worksheet is activated

Hybrid View

  1. #1
    Registered User
    Join Date
    03-25-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    21

    Worksheet_Activate () - Need to work when any worksheet is activated

    Hi All,

    I have this code sitting in Sheet2 of my workbook:

    Private Sub Worksheet_Activate()
    
    Application.ScreenUpdating = False
    
    Range("B:B").ClearContents
    
    For i = 1 To Sheets.Count
            Cells(i, 2) = Sheets(i).Name
        Next i
    Application.ScreenUpdating = False
    
    End Sub
    The code kicks in when I activate sheet2 (that is where I want the code to work), but what I need is for the code to update sheet2 whenever any of my worksheets are activated. Help...?

    JM

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Worksheet_Activate () - Need to work when any worksheet is activated

    Hi there,

    Insert the following code into the "ThisWorkbook" VBA CodeModule:


    
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    
        Const sTARGET_SHEET As String = "Sheet2"
    
        Dim wksTarget       As Worksheet
    
        Set wksTarget = ThisWorkbook.Sheets(sTARGET_SHEET)
    
        Application.ScreenUpdating = False
    
            With wksTarget
    
                .Range("B:B").ClearContents
    
                For i = 1 To Sheets.Count
                    .Cells(i, 2) = Sheets(i).Name
                Next i
    
            End With
    
        Application.ScreenUpdating = False
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    03-25-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Worksheet_Activate () - Need to work when any worksheet is activated

    Hi Greg,

    That worked perfectly, but it threw up an unexpected problem, because the code causes a small load time now every time I select a new tab, timings are building up - so could I ask you this: Instead of before when I wanted the code to kick in when any worksheet, can I have it kick in only when I select sheet3?

    Regards,
    JM

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Worksheet_Activate () - Need to work when any worksheet is activated

    Hi again Jim,

    You can speed up the execution of the routine a little bit by adding the highlighted statements:

    
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    
        Const sTARGET_SHEET As String = "Sheet2"
    
        Dim wksTarget       As Worksheet
    
        Set wksTarget = ThisWorkbook.Sheets(sTARGET_SHEET)
    
        Application.ScreenUpdating = False
    
            Application.Calculation = xlCalculationManual
    
                With wksTarget
    
                    .Range("B:B").ClearContents
    
                    For i = 1 To Sheets.Count
                        .Cells(i, 2) = Sheets(i).Name
                    Next i
    
                End With
    
            Application.Calculation = xlCalculationAutomatic
    
        Application.ScreenUpdating = False
    
    End Sub

    Or you could just change the first line of the routine to:

    
    Private Sub Worksheet_Activate()

    then insert the routine into the VBA CodeModule for Sheet 3, and delete it from the CodeModule for ThisWorkbook.

    And of course, you can even enter the "speeded-up" routine into just the CodeModule for Sheet 3.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ 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. Form with activated worksheet
    By jovir in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2013, 07:52 AM
  2. I can not get Worksheet_Activate to work
    By anwaee2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-18-2012, 01:41 PM
  3. [SOLVED] Worksheet_Activate Doesn't Work On Chart Sheet
    By splendidus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-18-2012, 04:55 AM
  4. how can worksheet_activate event of active sheet work of active workbook
    By tahir mahmood in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2011, 08:19 AM
  5. Run a macro when a Worksheet is clicked/activated
    By Warren McGoldrick in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2005, 06: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