Event Handler for switching from other applications to Excel?

后端 未结 3 933
心在旅途
心在旅途 2020-12-19 09:42

I want to activate a workbook when switching from other applications. I\'m using Excel 2010.

In the ThisWorkbook object, I\'ve tried the following:

         


        
相关标签:
3条回答
  • 2020-12-19 10:20

    OK I thought this was a job for Ribbon customization at first. I'm not able to do it with the Ribbon (not to say it is not possible, but I don't see any commandMSO's that would affect this functionality).

    Your class module like so (I did not experiment with the other view states that you had enumerated). This module encapsulates the event class and contains the application-level event handlers. For this purpose, I think you might only need the WorkbookActivate. The workbook raising the event will determine whether to enable/disable that property.

    Public WithEvents appevent As Application
    Dim ret As String
    Private Sub appevent_WorkbookActivate(ByVal wb As Workbook)
    
        Call ToggleDragAndDrop(wb, ret)
        'Comment out this line when satisfied it is working as expected
        MsgBox "Cell drag & drop enabled = " & ret
    End Sub
    

    Use the following in a standard module named mod_DragDrop:

    Option Explicit
    Public XLEvents As New cEventClass
    Sub SetEventHandler()
    
    If XLEvents.appevent Is Nothing Then
        Set XLEvents.appevent = Application
    End If
    
    End Sub
    
    Sub ToggleDragAndDrop(wb As Workbook, Optional ret$)
    
        Application.CellDragAndDrop = (wb.Name <> ThisWorkbook.Name)
        ret = Application.CellDragAndDrop
    End Sub
    

    Put this in the Workbook_Open event handler:

    Option Explicit
    Private Sub Workbook_Open()
        'Create the event handler when the workbook opens
        Call mod_DragDrop.SetEventHandler
        Call mod_DragDrop.ToggleDragAndDrop(Me)
    
    End Sub
    

    Note: If you "end" run-time or do anything while debugging which would cause state loss, you will lose the event handler. This can always be restored by calling the Workbook_Open procedure, so an additional safeguard might be to add this also in the ThisWorkbook code module:

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    ' Additional safeguard in case state loss has killed the event handler:
    ' use some workbook-level events to re-instantiate the event handler
    
        Call Workbook_Open
    End Sub
    

    I have made a copy of my file available on my Google Docs, just in case there is some errant typo in the code provided above.

    0 讨论(0)
  • 2020-12-19 10:31

    I guess after four years you won't have this question in mind still, so I just wanted to convert your comment into a complete answer, so that others have the answer a bit easier. The solution also works in Excel 2016.

    Private Sub Workbook_Open()
        'MsgBox "Opened and disabled"
        Application.CellDragAndDrop = False
    End Sub
    
    Private Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)
        'MsgBox "Activated and disabled"
        Application.CellDragAndDrop = False
    End Sub
    
    Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
        'MsgBox "Deactivated and enabled"
        Application.CellDragAndDrop = True
    End Sub
    
    Private Sub Workbook_Before_Close(Cancel As Boolean)
        'MsgBox "Closed and enabled"
        Application.CellDragAndDrop = True
    End Sub
    

    I posted this answer as community wiki, because you deserve the credit actually.

    0 讨论(0)
  • 2020-12-19 10:33

    It's in an option under the Advanced Tab in the Options --> "Enable fill handle and cell drag-and-drop".

    It's not VBA but does exactly what you want.

    0 讨论(0)
提交回复
热议问题