为什么清理VBA中的Excel单元格无法正常工作?
Excel 2003(其他未testing)
Private Sub Worksheet_Change(ByVal Target As Range) For Each cell In Target If Not Intersect(cell, Range("A2:A100")) Is Nothing Then cell.ClearContents cell.Offset(0, 1).Value = CInt(cell.Offset(0, 1).Value) + 1 End If Next cell End Sub
如果删除string
cell.ClearContents
它工作正常,下一个兄弟细胞增量,
但是随之而来的是兄弟细胞价值增加了几百
为什么?
如何正确地完成这项工作?
添加选项显式来保护你的vba没有未声明的variables,并消除编译错误的可能性
尝试这种方式应该工作
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim cell As Variant For Each cell In Target If Not Intersect(cell, Range("A2:A100")) Is Nothing Then cell.ClearContents cell.Offset(0, 1).Value = CInt(cell.Offset(0, 1).Value) + 1 End If Next cell End Sub