1

我对 VBA 很陌生,但很擅长公式。我正在处理时间戳问题。我编写了代码,因此如果我从 E3 中的验证列表中进行选择,它将在 F3 中给我一个时间戳。我希望 E 列中以 E3 开头的所有单元格都是如此。我将有 500 到 15000 条记录(行)。我正在使用的代码粘贴在下面。在此先感谢您的任何建议。

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column = 5 And Target.Row = 3 Then
    If Target.Value = "" Then
      Cells(3, 6).Value = ""
    Else
      Cells(3, 6).Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
    End If
  End If
End Sub
4

2 回答 2

3

这个怎么样?

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column = 5 And Target.Row >= 3 Then
  i = Target.Row
    If Target.Value = "" Then
  Cells(i, 6).Value = ""
Else
  Cells(i, 6).Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
End If
 End If
End Sub
于 2013-11-11T19:58:47.267 回答
0

最快的方法是选择整个范围并使用数组设置一次值。当它包含多个单元格时,这是通过.Valuea 的属性完成的。Range

Private Sub SetDate(ByVal Target As Range, Optional bybal RowCount as Long = 0)
    Dim i as Long
    ' Check if row count needs to be found
    If RowCount = 0 Then
        'Count non-empty rows from target down
        RowCount = Target.Worksheet.Range(Target, Target.End(xlDown).Rows.Count
    End If
    ' Target entire range of cells that are going to be affected
    Set Target = Target.Resize(RowCount, 6)
    Dim vals() as Variant 
    ' Read values from worksheet
    vals = Target.Values        
    ' Make changes in memory here
    For i=1 to RowCount
        if IsEmpty(vals(i,1)) Then
            vals(i, 6) = vbNullString
        Else
            vals(i, 6) = Format(Now, "mm/dd/yyyy HH:mm:ss")
        End If
    Next i
    ' Write values into worksheet
    Target.Value = vals
End Sub
于 2013-11-12T13:13:57.880 回答