Application.EnableEvents =false(true)的理解

來源:互聯網
上載者:User
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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.