Consulting

Results 1 to 13 of 13

Thread: Deactivate Event Causing Error

  1. #1
    VBAX Regular
    Joined
    Aug 2013
    Posts
    22
    Location

    Deactivate Event Causing Error

    I am running a Workbook_WindowDeactivate event that seems to be working incorrectly when the workbook is deactivated by opening a downloaded Excel file (in protected view). It works fine when I switch between workbooks or open a file from the directory.

    Error returned: Run-time error '1004' --- "Method 'Rows' of object '_Global' failed"

    The section of code causing the error in ThisWorkbook:
    ("Sheets(Logsheet)..." line in the if statement highlighted by debugger)

    Option Explicit
    Const LogSheet As String = "Log"
    Const BaseCol As String = "B"
    
    Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)   
        If Len(ThisWorkbook.Sheets("Log").Range("B2")) > 0 Then
            Sheets(LogSheet).Cells(Rows.Count, BaseCol).End(xlUp).Offset(0, 1).Value = Now
        End if
    End Sub
    Any ideas how to prevent this? Thanks

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Try fully qualifying this line
    ThisWorkbook.Sheets(LogSheet).Cells(Rows.Count, BaseCol).End(xlUp).Offset(0, 1).Value = Now

  3. #3
    VBAX Regular
    Joined
    Aug 2013
    Posts
    22
    Location
    It still returns the same error. I have also tried this via if statement: If ThisWorkbook.Name = ActiveWorkbook.Name Then... but that didnt work wither.

  4. #4
    What if:

    Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window) 
       Wn.Sheets("log").Cells(Rows.Count, 2).End(xlUp).Offset(, 1).Value = Now 
    End Sub

  5. #5
    VBAX Regular
    Joined
    Aug 2013
    Posts
    22
    Location
    Now im gettting this error on every deactivation:

    Run-Time error '438':

    Object doesn't support this property or method

  6. #6
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If the code is in the ThisWorkbook code page, try
    Private Sub Workbook_Deactivate()
    With ThisWorkbook.Sheets(LogSheet)
            If Len(.Range("B2")) > 0 Then .Cells(Rows.Count, BaseCol).End(xlUp).Offset(0, 1).Value = Now
    End With
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    This works for me. I was very explicit in referencing the WB and WS, including the dot in .Rows.Count, and some other objects.

    It can get tricking dealing with 2 different WBs or WSs, so I tend to not make Excel assume things


    Option Explicit
    Const LogSheet As String = "Log"
    Const BaseCol As String = "B"
     
    Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
        With ThisWorkbook.Sheets("Log")
            If Len(.Range("B2").Value) > 0 Then
                .Cells(.Rows.Count, BaseCol).End(xlUp).Offset(0, 1).Value = Now
            End If
        End With
    End Sub
    Paul

  8. #8
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    They both work for me. That is, both your's and mine
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Quote Originally Posted by SamT View Post
    They both work for me. That is, both your's and mine
    Yes, the OP can choose which event to hook: your WB_Deactivate or the Window_Deactivate one

    Personally, I almost always use the WB_Deactivate one like you did

    Paul

  10. #10
    This might also work:

    Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window) 
        Wn.Activesheet.Cells(Rows.Count, 2).End(xlUp).Offset(, 1).Value = Now 
    End Sub
    I don't think you have to address the workbook and the sheet specifically since they are inherent in this event:

    - Thisworkbook is equivalent to Workbook_ in the name of this eventcode
    - WN is a window in thisworkbook

    Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window) 
        msgbox Wn.parent.name
        msgbox Wn.activesheet.name
    End Sub
    Last edited by snb; 09-17-2013 at 07:12 AM.

  11. #11
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    @ snb

    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,722
    Location
    The error indicates that unqualified use of Rows.Count will not work so any solution needs to qualify that call. (this is often the case with workbooks opened in browser windows)
    Be as you wish to seem

  13. #13
    VBAX Regular
    Joined
    Aug 2013
    Posts
    22
    Location
    Adding the sheet reference to the 'With ThisWorkbook' statement fixed the issue. Thanks alot guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •