如果没有If构造来更改通过将范围内的单元格中的值删除为0而产生的空值,则以下代码将产生错误28堆栈溢出。
我对工作表更改事件的解释表明,删除将触发更改事件;但是,它们并不表示DEL将产生空的target.value并导致错误。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
If Target.Value = "" Then
Target.Value = 0
End If
MsgBox (Target.Value)
Set KeyCells = Range("C3:C10") ' Error occurs here
End Sub
确保删除范围中的值不会导致空值并产生此错误的最有效方法是什么?
答案 0 :(得分:1)
我不断重复说同样的三件事。
代码重写:
Option Explicit
Sub StartTimer()
Dim Start As Single, RunTime As Single
Dim ElapsedTime As String
'Set the control cell to 0 and make it green
Range("Y18").Value = 0
Range("Y14").Interior.Color = 5296274 'Green
Start = Timer ' Set start time.
Debug.Print Start
Do While Range("Y18").Value = 0
DoEvents ' Yield to other processes.
RunTime = Timer ' current elapsed time
ElapsedTime = Format((RunTime - Start) / 86400, "h:mm:ss")
'Display currently elapsed time in A1
Range("Y14").Value = ElapsedTime
Application.StatusBar = ElapsedTime
Loop
Range("Y14").Value = ElapsedTime
Range("Y14").Interior.Color = 192 'Dark red
Application.StatusBar = False
End Sub
Sub StopTimer()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
'Set the control cell to 1
Range("Y18").Value = 1
Set copySheet = Worksheets("MAIN")
Set pasteSheet = Worksheets("Time Spent")
copySheet.Range("Y14").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub