简体   繁体   English

VBA 运行脚本,用于当另一个工作簿中的特定单元格发生更改时

[英]VBA running script for when a specific cell in another workbook changes

Trying to create a script which detects when a cell in another workbook changes "B2" and once the change is detected it will run the macro RUNALL which has already been created and is working.尝试创建一个脚本来检测另一个工作簿中的单元格何时更改“B2”,一旦检测到更改,它将运行已经创建并正在运行的宏 RUNALL。 Runall will run a number of different macros, which saves as a pdf and sends an email to the customer. Runall 将运行许多不同的宏,这些宏保存为 pdf 并向客户发送 email。 Any help would be greatly appreciated任何帮助将不胜感激

    Sub Worksheet_Changes(ByVal Target As Range)

' Run the code when cell B2 is changed
If Target.Address = Workbook("M:\Wholesale\Test.xlsx").Sheet("Sheet1").Range("B2").Address Then


Call RUNALL

End If
End Sub

Read up on Application events: https://docs.microsoft.com/en-us/office/troubleshoot/excel/create-application-level-event-handler阅读应用程序事件: https://docs.microsoft.com/en-us/office/troubleshoot/excel/create-application-level-event-handler

In a class module clsAppEvents :在 class 模块clsAppEvents

Option Explicit

Private WithEvents app As Excel.Application
Private cellToMonitor As Range

Private Sub app_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Parent.Name = cellToMonitor.Worksheet.Parent.Name Then
        If Sh.Name = cellToMonitor.Worksheet.Name Then
            If Not Application.Intersect(Target, cellToMonitor) Is Nothing Then
                Debug.Print "Changed " & cellToMonitor.Address & " on " & _
                             Sh.Name & " in " & Sh.Parent.Name
            End If
        End If
    End If
End Sub

Private Sub Class_Initialize()
    Set app = Application
End Sub

Property Set TheCell(c As Range)
    Set cellToMonitor = c
End Property

In a regular module:在常规模块中:

Option Explicit

Private obj

Sub Tester()
    Set obj = New clsAppEvts
    Set obj.TheCell = Workbooks("Book2").Sheets(1).Range("A3") 'for example
End Sub

As BigBen has pointed out, the Worksheet_Changes is triggered only when there is a change in the sheet where your script is specifically located, therefore you should move the macro to the sheet's code located on the other workbook (the one that suffers the change).正如 BigBen 所指出的,仅当脚本所在的工作表发生更改时才会触发Worksheet_Changes ,因此您应该将宏移动到位于另一个工作簿(遭受更改的工作簿)上的工作表代码。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$2" Then
        Application.Run ("'M:\Wholesale\My_Book.xlsm'!RUNALL")
    End If
End Sub

Since the RUNALL macro will be located in a different workbook, you should use the Application.Run() method as findwindow also pointed out to make the reference to a different workbook.由于 RUNALL 宏将位于不同的工作簿中,因此您应该使用Application.Run()方法,因为 findwindow 还指出要引用不同的工作簿。 In this case "My_book.xlsm" is the one containing the RUNALL macro.在这种情况下,“My_book.xlsm”是包含 RUNALL 宏的那个。

Moreover, note that Workbook("M:\Wholesale\Test.xlsx").Sheet("Sheet1").Range("B2").Address will only return $B$2 making no difference between the two workbooks.此外,请注意Workbook("M:\Wholesale\Test.xlsx").Sheet("Sheet1").Range("B2").Address只会返回$B$2 ,这两个工作簿之间没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM