+ Reply to Thread
Results 1 to 6 of 6

use "Workbook_SheetDeactivate(ByVal Sh As Object)"

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2013
    Location
    Israel
    MS-Off Ver
    Excel 2010
    Posts
    2

    use "Workbook_SheetDeactivate(ByVal Sh As Object)"

    hi all,
    I have tried to use the "Workbook_SheetDeactivate(ByVal Sh As Object)".
    the reason I want to use it is to be able to switch from current sheet to the previous one.

    so what I did was declaring "lastsheet" variable : Public LastSheet As Worksheet
    then I used this in "ThisWorkbook" module :

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Set LastSheet = Sh
    End Sub


    the problem is that I don't know what to do next. every time I declaring a new subroutine and use Lastsheet.select then I get an error when calling this sun within a Macro.
    so, what am I doing wrong? is there an easier way? or any other working way ?

    thank you very much.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: use "Workbook_SheetDeactivate(ByVal Sh As Object)"

    Code in Thisworkbook module

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    
        If g_objLastSheet Is Nothing Then Exit Sub
    
        Set g_objLastSheet = Sh
        
    End Sub
    Standard code module
    Sub MoveBack()
    
    Debug.Print "Before", ActiveSheet.Name, g_objLastSheet.Name
        g_objLastSheet.Activate
    Debug.Print "After", ActiveSheet.Name, g_objLastSheet.Name
        
    End Sub
    No idea how you are using it though
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: use "Workbook_SheetDeactivate(ByVal Sh As Object)"

    Code in Thisworkbook module

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    
        If g_objLastSheet Is Nothing Then Exit Sub
    
        Set g_objLastSheet = Sh
        
    End Sub
    Standard code module
    Sub MoveBack()
    
    Debug.Print "Before", ActiveSheet.Name, g_objLastSheet.Name
        g_objLastSheet.Activate
    Debug.Print "After", ActiveSheet.Name, g_objLastSheet.Name
        
    End Sub
    No idea how you are using it though

  4. #4
    Registered User
    Join Date
    11-06-2013
    Location
    Israel
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: use "Workbook_SheetDeactivate(ByVal Sh As Object)"

    Quote Originally Posted by Andy Pope View Post
    Code in Thisworkbook module

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    
        If g_objLastSheet Is Nothing Then Exit Sub
    
        Set g_objLastSheet = Sh
        
    End Sub
    Standard code module
    Sub MoveBack()
    
    Debug.Print "Before", ActiveSheet.Name, g_objLastSheet.Name
        g_objLastSheet.Activate
    Debug.Print "After", ActiveSheet.Name, g_objLastSheet.Name
        
    End Sub
    No idea how you are using it though
    thank you.
    when running the Moveback() , I'm getting a runtime error '424' object required..
    any ideas?

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: use "Workbook_SheetDeactivate(ByVal Sh As Object)"

    Duplicate post!

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: use "Workbook_SheetDeactivate(ByVal Sh As Object)"

    Did the routine have anywhere to move back to? What did the debug message say was the Before sheet names.

    Include the test for is nothing if you are trying to moveback without first having deactivated a 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. Need help creating VBA looping code in "Worksheet_Change(ByVal Target As Range)"
    By jimredfield in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-11-2012, 03:09 PM
  2. "The "sheets" method from the "_Global" object have failed."
    By mankit87 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2011, 08:53 AM
  3. Error msgs: "Object varible or with block variable not set"; "subscript out of range"
    By menyanthe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-26-2009, 04:58 PM
  4. 2 target cells for "Change(ByVal..." worksheet macro
    By timmtamm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-12-2009, 05:12 PM
  5. What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 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