我有这个函数试图检测特定单元格值何时发生变化。问题是,如果用户选择整个电子表格并按下删除,我会在检查范围只是一个单元格时出现溢出:
Public Sub Worksheet_Change(ByVal Target As Range)
'Overflow can occur here if range = whole spreadsheet
If Not IsError(Int(Target.Cells.Count)) Then
If Target.Cells.Count = 1 Then
If Target.Cells.Row = 4 And Target.Cells.Column = 1 Then
Sheets("X").Cells(5, 1).Calculate
End If
End If
End If
End Sub
当单个特定单元格值发生更改时,是否有更优雅的方法可以让此代码仅运行? (没有溢出,清除整个工作表时出现问题等)?
最佳答案
我假设您使用的是 Excel 2007+,因为这些版本中的行数和列数急剧增加。您可能会更幸运地检查以确保行数和列数 = 1,因为这些最大值将远低于两者的乘积(即单元格数):
If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
关于vba - 检查一个范围是不是整个工作表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690580/