6

我想在从其他应用程序切换时激活工作簿。我正在使用 Excel 2010。

在 ThisWorkbook 对象中,我尝试了以下操作:

Private Sub Workbook_Activate()
    MsgBox "1"
End Sub

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    MsgBox "2"
End Sub

在一个类模块中,我尝试了这些:

Public WithEvents appevent As Application
Private Sub appevent_ProtectedViewWindowActivate(ByVal Pvw As ProtectedViewWindow)
    MsgBox "1"
End Sub

Private Sub appevent_ProtectedViewWindowOpen(ByVal Pvw As ProtectedViewWindow)
    MsgBox "2"
End Sub

Private Sub appevent_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window)
    MsgBox "3"
End Sub

Private Sub appevent_WorkbookActivate(ByVal Wb As Workbook)
    MsgBox "4"
End Sub

Private Sub appevent_WorkbookDeactivate(ByVal Wb As Workbook)
    MsgBox "5"
End Sub

要求是在激活此工作簿(单击或 alt-tabbed-to)时禁用 CellDragAndDrop 属性,并在此工作簿不活动时重新启用它。

4

3 回答 3

2

好的,我一开始以为这是功能区定制的工作。我无法用功能区做到这一点(并不是说不可能,但我没有看到任何会影响此功能的 commandMSO)。

你的类模块是这样的(我没有尝试你列举的其他视图状态)。该模块封装了事件类并包含应用程序级事件处理程序。为此,我认为您可能只需要WorkbookActivate. 引发事件的工作簿将确定是否启用/禁用该属性。

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

在名为 的标准模块中使用以下内容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

把它放在Workbook_Open事件处理程序中:

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

注意:如果您“结束”运行时或在调试时做任何会导致状态丢失的事情,您将丢失事件处理程序。这始终可以通过调用 Workbook_Open 过程来恢复,因此额外的保护措施可能是在ThisWorkbook代码模块中添加它:

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

我已经在我的Google Docs上提供了我的文件的副本,以防万一上面提供的代码中有一些错误的错字。

于 2014-08-18T19:04:28.930 回答
1

我想四年后您仍然不会想到这个问题,所以我只是想将您的评论转换为完整的答案,以便其他人更容易获得答案。该解决方案也适用于 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

我将此答案发布为社区 wiki,因为您实际上值得称赞。

于 2018-08-23T12:52:58.237 回答
1

它位于选项 - >“启用填充手柄和单元格拖放”中的“高级”选项卡下的一个选项中。

它不是 VBA,但正是你想要的。

于 2019-04-26T01:51:49.307 回答