检测纸张更换,清除纸张溢出

我有一个Worksheet_Change事件,目前在Sheet Module Level 。 问题是,我希望能够有时清除此表。 但是,当我清除我的表,我得到一个溢出:

 Private Sub Worksheet_Change(ByVal Target As Range) 'This is the line causing the problem because clearing the whole sheet causes the count to be massive If Target.Count = 1 Then If Target = Range("A4") Then If InStr(LCase(Target.Value), "loaded") <> 0 Then Range("A5").FormulaArray = "=My_Function(R[-1]C)" End If End If End If End Sub 

我正在尝试实现以下内容:

我按下一个button,工作表被清除( 清除现有的数组公式数据 ),然后将公式粘贴到工作表中并调用公式。 公式将数据返回到Excelcaching并将包含此公式(A4)的单元格更改为一个string,指示“已加载”。 当我检测到一个单元格的值改变“加载”,然后按Ctrl + Shift + Enter上的数组公式function下面的显示数据。

我相信你正在使用xl2007 +?

Target.Cells.Count是一个Long值,因此当您select整个工作表时, .Count太小而无法保存结果。

换行

 If Target.Count = 1 Then 

 If Target.Cells.CountLarge = 1 Then 

你可能也想看到这个,因为你正在使用Worksheet_Change

编辑

另外两件事

1)

你也可以replace这一行

 If Target = Range("A4") Then 

 If Not Intersect(Target, Range("A4")) Is Nothing Then 

2)

这条线

 If InStr(LCase(Target.Value), "loaded") <> 0 Then 

也可以写成

 If InStr(1, Target.Value, "loaded", vbTextCompare) Then