[英]Excel runs VBA code and then crashes
多年来,我一直在各种版本的Excel中使用此简单代码,直到2010年,这都是为了在一张纸上添加时间戳记,通过在A列中输入数字,输入时间将插入到B列中的相邻单元格中。我最近购买了一台平板电脑,因为这样可以更好地使用电子表格。 平板电脑正在运行Windows 8和Office2013。当我运行工作表时,将时间输入到单元格中,但是随后立即出现消息“ Microsoft Excel已停止工作”并且Excel关闭。 我已经在平板电脑上加载了Excel 2010,因为我认为问题可能出在Excel 2013,但这还是行不通的。
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim c As Integer
Dim f As Integer
c = ActiveCell.Column
r = ActiveCell.Row
If c <> 1 Then End
Cells(r - 1, c + 1) = Time$
Cells(r - 1, c + 1).NumberFormat = "h:mm:ss AM/PM"
End Sub
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim c As Long
Dim f As Long
c = ActiveCell.Column
r = ActiveCell.Row
If c <> 1 Then Exit Sub
If r = 1 Then Exit Sub
Application.EnableEvents = False
Cells(r - 1, c + 1) = Now
Cells(r - 1, c + 1).NumberFormat = "h:mm:ss AM/PM"
Application.EnableEvents = True
End Sub
另一种较短的版本是
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not (Intersect(Target, Range("A2", Range("A2").End(xlDown))) Is Nothing) Then
Application.EnableEvents = False
Target.Offset(0, 1) = Now
Target.Offset(0, 1).NumberFormat = "h:mm:ss AM/PM"
Application.EnableEvents = True
End If
End Sub
在第1列中输入多个值时,此解决方案也适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.