在Excel中的工作表更改事件上有错误吗

在Excel中的工作表更改事件上有错误吗

本文介绍了在Excel中的工作表更改事件上有错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sheet1模块中的简单命令:

The simply command in the Sheet1 module:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Range("J5:M5").ClearContents
End Sub

导致Excel崩溃.

推荐答案

您将陷入无限循环.清除单元格时,将重新激活宏,然后在清除单元格时,将重新激活宏,然后在清除单元格时.....

You are going into a infinite loop. When you clear your cell, you reactivate the macro, and then when you clear your cell, you reactivate the macro, and then when you clear your cell.....

在进行更改之前禁用事件,然后使用

Disable events before you make your change and the re-enable them with

Application.EnableEvents = False
    Range("J5:M5").ClearContents
Application.EnableEvents = True


您可能还需要考虑将其应用于特定范围.您是否真的要在此工作表上进行任何更改以触发您的宏?还是您正在寻找特定地区的变化?如果是这样,请指定区域,并在该区域Intersects(重叠)与更改的单元格一起运行时运行宏.


You may also want to consider applying this to a specific range. Do you really want any change on this sheet to trigger your macro? Or are you looking for a change in a specific region? If so, specify the region, and run the macro when that region Intersects (overlaps) with the changed cell.

这篇关于在Excel中的工作表更改事件上有错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:21