簡體   English   中英

Excel運行VBA代碼,然后崩潰

[英]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
  1. 更改DIM的
  2. 錯誤測試
  3. 避免再次進入

另一種較短的版本是

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM