Application.EnableEvents =false(true)的理解

Private Sub Worksheet_Change(ByVal Target As Range)

Dim iRow As Integer

iRow = Target.Row

Application.EnableEvents = False

Cells(iRow, 3) = Cells(iRow, 3) + Cells(iRow, 2)

Application.EnableEvents = True

End Sub

这个程序的目的是要在 B2 输入新的数时,C2 会将 B2 输入的新数加上 C2 原有的数呈现在 C2 上。

照上面有加上 Application.EnableEvents = False 程序执行当然没问题。

现在你在 Application.EnableEvents = False 与 Application.EnableEvents = True 前加上「 '」看看(下面代码)。

程序前加上「 '」的目的是要使「 '」之后的文字变成说明文字,程序执行时是会跳过说明文字,不执行说明文字的内容。

程序前加上「 '」符号后,文字会变成绿色。

执行第二个程序时,你将发现 C2 不会按你所要求的,呈现结果。

这就是所谓的事件连锁反应。


Private Sub Worksheet_Change(ByVal Target As Range)

Dim iRow As Integer

iRow = Target.Row

'Application.EnableEvents = False

Cells(iRow, 3) = Cells(iRow, 3) + Cells(iRow, 2)

'Application.EnableEvents = True

End Sub

再如:

本示例在保存文件之前禁用事件,以使 BeforeSave 事件不能触发。


Application.EnableEvents = False
ActiveWorkbook.Save
Application.EnableEvents = True

 
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim NodX As Node Dim accTypeDic As Object Dim arr5(), arr6() If Me.CmdSwitch.Caption = "控件输入" Then Exit Sub If Target.Row > 1 Then '避开表头 If Selection.Cells.count > 1 Then '避开多重选区 Me.TextBox1.Visible = False Me.ListBox1.Visible = False Exit Sub End If If Target.Column = 4 Then '科目 Set accTypeDic = CreateObject("Scripting.Dictionary") iRow = Sheets("科目").UsedRange.Rows.count arr = Sheets("科目").Range("A2:F" & iRow).Value For i = 1 To UBound(arr, 1) If Len(arr(i, 5)) = 1 Then accTypeDic(arr(i, 5)) = arr(i, 6) End If Next arr5 = accTypeDic.keys arr6 = accTypeDic.items With Me.TreeView1 .Nodes.Clear .Visible = True .Top = Target.Top .Left = Target.Left + Target.Width .Height = Target.Height * 10 .Width = Target.Width Set NodX = .Nodes.Add(, , "Account", "科目列表") For i = LBound(arr5) To UBound(arr5) Set NodX = .Nodes.Add("Account", tvwChild, "A" & arr5(i), arr6(i)) NodX.EnsureVisible Next For i = 1 To UBound(arr, 1) If Len(arr(i, 1)) >= 4 Then Set NodX = .Nodes.Add("A" & arr(i, 5), tvwChild, "A" & arr(i, 1), arr(i, 3)) End If Next End With Me.TextBox1.Visible = False Me.ListBox1.Visible = False ElseIf Target.Column = 5 Then '现金流量 Me.TreeView1.Visible = False iRow = Sheets("XJLL").UsedRange.Rows.count arr = Sheets("XJLL").Range("C2:C" & iRow).Value If Left(Target.Offset(0, -1), 4) = "1001" Or Left(Target.Offset(0, -1), 4) = "1002" Then With Me.TextBox1 .Visible = True .Top = Target.Top .Left = Target.Left + Target.Width .Height = Target.Height .Width = Target.Width .Text = IIf(IsNull(Target.Text), "", Target.Text) .Activate End With With Me.ListBox1 .Visible = True .Top = Me.TextBox1.Top + Me.TextBox1.Height .Left = Me.TextBox1.Left .Height = Target.Height * 5 .Width = Target.Width If Me.TextBox1 = "" Then .List = arr End If End With Else Me.TextBox1.Visible = False Me.ListBox1.Visible = False End If Else Me.TextBox1.Visible = False Me.ListBox1.Visible = False Me.TreeView1.Visible = False End If Else Me.TextBox1.Visible = False Me.ListBox1.Visible = False Me.TreeView1.Visible = False End If
最新发布
03-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值