excel私人子工作表_change不起作用

我在Excel工作表中有一个VBA子应该在给定的列中查找一个变化,如果它看到一个,添加date和时间(从Now() )到不同列中的相应单元格。 这个子检查两个地方的变化,取决于哪个范围被改变,它更新一个不同的单元格(如果列G改变,更新列A;如果列K改变,更新列M)。

我没有得到任何错误或任何东西,date和时间只是没有被添加到相应的单元格。 我的代码如下。 我不能为了我的生活找出什么是错的。 它工作了几天前,我一直没有能够得到它的工作。

 Private Sub Worksheet_Change(ByVal Target As range) Dim cell As range 'Adds unique keyA values 'Check to see if the changed cell is in column G If Not Intersect(Target, range("G:G")) Is Nothing Then For Each cell In Target.Cells If cell.Value <> vbNullString And Target.Row > 7 And Target.Row <= 20 Then 'Update the "KeyA" value sheets("Front End").range("A" & Target.Row).Value = Now() End If Next cell Else 'Adds unique keyB values 'Check to see if the changed cell is in column K If Not Intersect(Target, range("K:K")) Is Nothing Then For Each cell In Target.Cells If cell.Value <> vbNullString And (Target.Row > "7" And Target.Row <= "27") Then 'Update the "KeyM" value sheets("Front End").range("M" & Target.Row).Value = Now() End If Next cell End If End If End Sub 

更改G行中的值的代码由一个button调用,如下所示:

 Private Sub CommandButton1_Click() Sheets("Front End").Unprotect ("29745") h = Hour(Now) For Each c In range("B8:B20") If h = Hour(c) Then c.Offset(0, 3) = CInt(c.Offset(0, 3)) + 1 Exit For End If Next c Sheets("Front End").Protect ("29745") Unload Me End Sub 

也许你正试图达到这样的事情:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim cell As Range 'Adds unique keyA values 'Check to see if the changed cell is in column G If Not Intersect(Target, Range("G:G")) Is Nothing Then For Each cell In Target.Cells If cell.Value <> vbNullString And Target.Row > 7 And Target.Row <= 20 Then 'Update the "KeyA" value Range("A" & Target.Row).Value = Now() End If Next cell Else 'Adds unique keyB values 'Check to see if the changed cell is in column K If Not Intersect(Target, Range("K:K")) Is Nothing Then For Each cell In Target.Cells If cell.Value <> vbNullString And (Target.Row > "6" And Target.Row <= "27") Then 'Update the "KeyM" value Range("M" & Target.Row).Value = Now() End If Next cell End If End If End Sub 

Worksheet_Change不是Selection_Change。