如果macros2正在运行,则取消macros1
我有两个macros,一个macros分配给我的Private Worksheet_Change event
,另一个分配给我的Private Worksheet_SelectionChange
事件像这样:
macros1
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'If Hours Column Selected If Not Intersect(Target, Range("Z" & ActiveCell.Row)) Is Nothing And Range("Z" & ActiveCell.Row).Value <> "" Then NewValue = Application.InputBox("Please Enter Your Delegated Reference:") If NewValue <> vbNullString Then Dim rw2 As Long, cell2 As Range rw2 = ActiveCell.Row With Worksheets("Data").Columns("I:I") Set cell2 = .find(What:=NewValue, LookIn:=xlFormulas, _ LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False) If Not cell2 Is Nothing Then Application.DisplayAlerts = False cell2.Offset(0, 4).Value = Sheet1.Range("Y" & ActiveCell.Row).Value cell2.Offset(0, 5).Value = Sheet1.Range("H" & ActiveCell.Row).Value cell2.Offset(0, 6).Value = Sheet1.Range("I" & ActiveCell.Row).Value MsgBox "Found" Sheet1.Range("Y" & ActiveCell.Row).Value = cell2.Offset(0, 1).Value Sheet1.Range("H" & ActiveCell.Row).Value = cell2.Offset(0, 2).Value Sheet1.Range("I" & ActiveCell.Row).Value = cell2.Offset(0, 3).Value Application.DisplayAlerts = True Else MsgBox "Not Found" Sheet1.Range("A5").Select End If End With Else If NewValue = vbNullString Then MsgBox "Not Found" Sheet1.Range("A5").Select End If End If End If End Sub
macros2
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("Y" & ActiveCell.Row)) Is Nothing Then myValue3 = MsgBox("This is a message") End If End Sub
我遇到的问题是,当我点击ZI列中的活动单元行,正在运行macros1,并要求它更新列Y中活动单元行的值。但是,当列Y中的信息更新时,它正在导致macros2运行,我得到一个msgbox显示,我不希望这发生。
虽然我仍然需要macros2,并希望msgbox显示,我只希望它显示,当我点击单元格列Y.所以换句话说,我想能够取消macros2如果macros1运行。
我曾尝试在macros1中使用application.displayevents = false
,但是这不起作用。
请有人可以告诉我最好的方法来做到这一点?
您可以使用
Application.EnableEvents = False
..在你的macros1的开始禁用事件和
Application.EnableEvents = True
在macros1结束时再次打开它。
请尝试以下操作:
[1]添加一个新模块到您的VBA项目中,具体如下:
Public EventRunning as Boolean
[2]修改您的Worksheet_SelectionChangemacros,如下所示:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) EventRunning=True ... EventRunning=False End Sub
[3]修改您的Worksheet_Change,如下所示:
Private Sub Worksheet_Change(ByVal Target As Range) If EventRunning Then Exit Sub ...
祝本