如何让Excel电子表格自动填充日期&时间使用VBA?

时间:2015-02-18 01:28:50

标签: vba excel-vba excel

尝试在Excel上获取启用宏的工作表,以便在B列或C列中输入任何值时自动填充日期和时间。

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Integer
  For i = 2 To 100
    If Cells(i, "B").Value <> " " And Cells(i, "C").Value = " " Then
      Cells(i, "F").Value = Date & " " & Time
      Cells(i, "F").NumberFormat = "m/d/yyyy h:mm AM/PM"
    End If
  Next
  Range("F:F").EntireColumn.AutoFit
End Sub

我写的代码有什么问题吗?

3 个答案:

答案 0 :(得分:1)

&#34;目标&#34;将是改变的细胞。可以一次更改多个单元格(通过ctrl-enter),因此检查目标中的所有单元格并不是一个坏主意。

如果您使用Intersect方法,它将只获得Target的区域和您要检查的重叠范围。然后,这将循环遍历这些单元格(如果有的话),如果找到值,则为它们加时间戳。

正如其他人所提到的,在插入邮票之前禁用事件将阻止调用另一个工作表更改事件。调试时要小心,不要关闭事件。

您可以在此处详细了解事件参数:https://msdn.microsoft.com/en-us/library/office/ff839775.aspx

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Excel.Range
    Dim cll As Excel.Range
    Set rng = Excel.Intersect(Target, Range("B:C"))
    If Not (rng Is Nothing) Then
        Excel.Application.EnableEvents = False
        For Each cll In rng.Cells
            If Len(cll.Formula) > 0 Then
                Cells(cll.Row, 6).Value = Format$(Now, "m/d/yyyy h:mm AM/PM")
            End If
        Next
        Range("F:F").EntireColumn.AutoFit
        Excel.Application.EnableEvents = True
    End If
End Sub

答案 1 :(得分:1)

每次工作表上的任何内容发生变化时,您都不希望完成所有这些操作;只有当影响时间戳有效性的内容发生变化时。通常,我们会使用Intersect来确定更改的其中一个值是否应该接收新的时间戳。您也不希望例程尝试在自身上运行,因此建议在更改值之前关闭事件处理(即添加时间戳)。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B:C")) Is Nothing Then
        On Error GoTo SafeExit
        Application.EnableEvents = False
        Dim bc As Range   'no sense in declaring something until we actually need it
        For Each bc In Intersect(Target, Range("B:C")) 'deal with every cell that intersects. This is how to handle pastes into more than one cell
            If Not IsEmpty(Cells(bc.Row, "B")) And Not IsEmpty(Cells(bc.Row, "C")) Then
                Cells(bc.Row, "F").Value = Now 'Now is the equivalent of Date + Time
                Cells(bc.Row, "F").NumberFormat = "m/d/yyyy h:mm AM/PM"
            End If
        Next bc
        'Range("F:F").EntireColumn.AutoFit 'this slows things down. you may want to comment this out and just set an apprpriate column width that will handle everything
    End If
SafeExit:
    Application.EnableEvents = True
End Sub

这是我对这个老问题的看法。有很多例子。请查看本页右侧的相关部分,以获取一些链接。

答案 2 :(得分:0)

一些微小的变化:

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Integer
  Application.EnableEvents = False
    If Target.Column = 2 Or Target.Column = 3 Then
      For i = 2 To 100
        If Cells(i, "B").Value <> " " And Cells(i, "C").Value = " " Then
          Cells(i, "F").Value = Date & " " & Time
          Cells(i, "F").NumberFormat = "m/d/yyyy h:mm AM/PM"
        End If
      Next
    End If
  Range("F:F").EntireColumn.AutoFit
  Application.EnableEvents = True
End Sub

关闭偶数,以便在代码进行修改时不要触发它并测试目标列以查看它是否为B或C并且仅在它是

时触发

另外,您知道您的代码会更新第2行到第100行,而不管哪一行被更改了吗?如果您只想要更改的行,可以使用target.row