Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim c As Range
For Each c In Target
If Target.Column = 4 Then
Dim i%, j%, x%
i = Range("c65536").End(xlUp).Row '取得c列最后一个非空单元格行号
If i < 3 Then Exit Sub '当没有数据,只有标题行存在时,退出程序
For x = 3 To i
If Cells(x, c) <> "" Then j = j + 1: Cells(x, 1) = j '判断c列是否为空,不为空时,序号j+1写入对应A单元格
Next x
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Target
If c.Column = 4 Then ' 源代码是 Target.Column,错。另外,确定是 4?4 对应 D 列哦。
Dim i%, j%, x%
i = Range("c65536").End(xlUp).Row
If i < 3 Then Exit Sub
For x = 3 To i
If Cells(x, "C") <> "" Then j = j + 1: Cells(x, 1) = j ' 源代码是 Cells(x, c),要表示 C 列,必须加双引号!
Next x
End If ' 源代码缺少 end if 收尾,if 语法不对
Next ' 源代码缺少 next 收尾,for each 语法不对
End Sub