【问题标题】:excel private sub worksheet_change not workingexcel私人子工作表_更改不起作用
【发布时间】:2016-06-16 12:51:46
【问题描述】:

我的 Excel 工作表中有一个 VBA 子程序,它应该在给定列中查找更改,如果发现更改,请将日期和时间(来自 Now())添加到不同列中的相应单元格。该子程序在两个位置检查更改,并根据更改的范围更新不同的单元格(如果 G 列更改,则更新 A 列;如果 K 列更改,则更新 M 列)。

我没有收到任何错误或任何内容,只是没有将日期和时间添加到相应的单元格中。我的代码如下。我无法终生弄清楚它有什么问题。几天前它工作了,从那以后我一直无法让它工作。

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行值的代码由一个按钮调用,如下:

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

【问题讨论】:

  • 函数是否在正确的工作表下,是否被调用?
  • 数据是如何改变的?手动,还是通过公式重新计算?
  • 函数在正确的工作表下,没有被调用。当用户点击用户表单中的按钮时,数据正在发生变化
  • 您没有指定要更新的工作表。如果用户正在使用表单,那么 ActiveSheet 可能不是您认为的那样;使用您要更新的工作表的名称完全限定您的 Ranges 和 Cells 等。
  • 按钮代码如何更新G列? B 列加上三列 (Offset(0,3)) 就是 E 列,对吧? --- 无论如何,尝试删除保护。它可能会阻止Worksheet_Change 更改受保护的工作表。

标签: vba excel date


【解决方案1】:

可能你正试图实现这样的目标:

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。

【讨论】:

  • 它们有什么不同,用户不是选择一个单元格并改变它,一个公式是在一个子单元对不同单元格的值加减1后改变单元格
  • 没有修复它,我做了第一行“selectionChange”,当这些值发生变化时它仍然不会更新 A 或 M 列
  • @Vbasic4now 在您的代码中,您还应该更改第 7 行和第 20 行之间的行中的值。我已经检查了代码,它工作正常。尝试使用一些停止单元格,看看你在哪里逃脱了 if-s。
猜你喜欢
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多